Customizing SMF > SMF Coding Discussion

Need some help with an array

(1/6) > >>

cicka:
Hi guys,

I am coding a mod that will add multiple tabs in the navbar. Everything has gone great so far but I am stuck in the very last part.

This is where I have run into difficulties. When I try to show up the custom tabs.


--- Code: ---
$clinks = $smcFunc['db_query']('', '
SELECT  *
FROM {db_prefix}custom_links
WHERE link_id = {int:link_id}',
);

foreach($clinks as $clink)
{

'clinks' => array(
'title' => $clink['c_links'],
                                'href' => $scripturl . '?action=clinks',

),
),
);
--- End code ---

It displays only the latest link that I create. The links before that will not be displayed. I am totally lost as to why it will not be displayed. I have been working on this all week-end but no joy so far. Any input will be much appreciated.

Thank you very much :)

Arantor:
Well, there's a missing db_fetch_assoc in the middle there ;)

But the real part of your issue is that it's an associative array. When you declare two elements in an associative array with the same name, it overwrites the first one.

So, on the first iteration of the foreach(), the menu's clinks elements is made. On the second iteration it's overwritten with the new title/href array, and so on.

You need to give them unique keys for the navbar, and remember also that in the navbar, action=item expects the 'item' element, in order to highlight it. So in the above case, going to action=clinks would highlight your tab.

cicka:
Thank you Arantor. Can you please post the exact code that I have to add there? I have tried every method that I could think of and I failed.

Yoshi:
Implementing everything Arantor said:


--- Code: ---$clinks = $smcFunc['db_query']('', '
SELECT  *
FROM {db_prefix}custom_links
WHERE link_id = {int:link_id}',
);

$links = array();
foreach($smcFunc['db_fetch_assoc']($clinks) as $clink)
{
                        $links[] = array(
                            'title' => $clink['c_links'],
                            'href' => $scripturl . '?action=clinks',
),
);
--- End code ---

Good luck with your mod :)

Arantor:
Apart from the fact that the above code is wrong (that will give you one iteration per column of the table row, which is really not what you want), it also doesn't deal with the highlighting of menu items.

What should be highlighted when exactly?

Navigation

[0] Message Index

[#] Next page

Go to full version