0

I have the following script currently, which gets the category IDs as arrays:

$sql_select_categories = $db->query("SELECT category_id FROM " . DB_PREFIX . "categories 
                                     WHERE parent_id='" . intval($src_details['parent_id']) . "' 
                                     ORDER BY order_id ASC, name ASC"); 

$additional_vars = set_filter_link($src_details, array('parent_id' => '', 'start' => ''), 'address');

while ($cat_details = $db->fetch_array($sql_select_categories)) {
    $cat_array[$cat_details['category_id']]["name"] = $category_lang[$cat_details['category_id']];
}

if(is_array($cat_array)) {  
    asort($cat_array);

    foreach($cat_array as $key => $value) {     
        $subcat_link = basename($_SERVER['PHP_SELF']) . '?parent_id=' . $key . $additional_vars;

        $output .= '<tr> '.
        '   <td class="contentfont">&nbsp;&raquo; <a href="' . $subcat_link . '">' . $category_lang[$key] . '</a></td> '.
        '</tr> ';
    }
}   
return $output;

This works perfectly, except I need to extract one more variable from the database which is called count. So the MySQL query will change from

SELECT category_id FROM

to

SELECT category_id, count FROM

So far so good, but how do I get it to then display each of the counts in the foreach? I need them displayed in the HTML after $category_lang[$key] as something like $count.

1 Answer 1

1

Your $cat_details array will have another element that you can reference, which will be $cat_details['count']. You can then add that to your $cat_array like you do with that name element in the while loop.

$cat_array[ $cat_details['category_id'] ]['count'] = $cat_details['count'];

Make sense?

Sign up to request clarification or add additional context in comments.

5 Comments

First of all thank you! Makes sense but something isn't working :( Do I add the $cat_array for the count as a 2nd line? Or somehow build it into the other one. Sorry, I just started learning with arrays and it's all very complicated!
Do I change it to something like this?
I got it working by adding another line of $cat_array below the other one. I printed the entire array in my browser and I see it's working, but can't figure out how to then print it as a $value.
It now displays the values as: [215] => Array ( [name] => Category Name [items_counter] => 19 )
Sorry for the delay! Each $value will be an array, as well, so you will reference $value['count'] in the output foreach loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.