Customizing SMF > SMF Coding Discussion

Need some help with an array

<< < (2/6) > >>

Yoshi:

--- Quote from: Arantor on July 29, 2012, 05:02:06 PM ---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.
--- End quote ---
Huh, you lost me there.


--- Quote ---What should be highlighted when exactly?

--- End quote ---
As the query says * instead of all the keys I have no idea which one handles the highlighting.

Arantor:

--- Quote ---Huh, you lost me there.
--- End quote ---

Let me rewrite your code more clearly and maybe you'll see why it's not what you wanted it to be.


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

$links = array();

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

Doing it in the foreach means it simply runs once, returns a single array, then loops over that array, meaning you add one new tab for each column of the array, which is one column of the database table.


--- Quote ---As the query says * instead of all the keys I have no idea which one handles the highlighting.
--- End quote ---

I'm not being funny but I wasn't asking you what your code was doing. I was asking what the OP wanted to do, because so far none of the code is right. Depending on what is desired to be highlighted may or may not mean fudging the columns and $current_action later on.

cicka:
That explains why I always get only the latest tab to display instead of everyone of them. I will try it now the suggestions posted above and will report back.

I can not thank you guys enough for your precious help :)

Yoshi:

--- Quote from: Arantor on July 29, 2012, 05:08:06 PM ---
--- Quote ---Huh, you lost me there.
--- End quote ---

Let me rewrite your code more clearly and maybe you'll see why it's not what you wanted it to be.


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

$links = array();

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

Doing it in the foreach means it simply runs once, returns a single array, then loops over that array, meaning you add one new tab for each column of the array, which is one column of the database table.
--- End quote ---
Xcuse me, I was confusing that with a while loop, which works better in this case.


--- Quote ---
--- Quote ---As the query says * instead of all the keys I have no idea which one handles the highlighting.
--- End quote ---

I'm not being funny but I wasn't asking you what your code was doing. I was asking what the OP wanted to do, because so far none of the code is right. Depending on what is desired to be highlighted may or may not mean fudging the columns and $current_action later on.

--- End quote ---
For which I'm well aware. This is one of the reasons why I hate that people use wildcards in their queries.

Arantor:

--- Quote ---Xcuse me, I was confusing that with a while loop, which works better in this case.
--- End quote ---

Just a bit.


--- Quote ---For which I'm well aware. This is one of the reasons why I hate that people use wildcards in their queries.
--- End quote ---

Apparently not, given that you're now thoroughly confusing two very different things entirely.

Wildcards have a place, but they're best avoided in the interests of clarity and performance. They make precisely zero difference in this case however.

The problem here that I'm alluding to would be no different whether it was SELECT * or SELECT column1, column2, column3. The problem is the array key that's the new tab container element. If it's in a position where it is going to be highlighted by an action, it has to be the same as that action. If it's merely a link to an external site, it doesn't matter what its id is provided that it is unique (and ideally not an existing action id), nothing whatsoever to do with SELECTing with wildcards.

(Did you know, SMF itself even does it under one circumstance, when it selects mem.* in loading the current user's data? It is also important in cases where the exact structure may not be known in advance, though in those cases, interrogating the structure of the table would be preferable anyway)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version