0

I have a piece of code that is supposed to grab categories, sort them alphabetically (they're available in several languages) as well as return a count for each of them. The problem is, for some reason the count isn't returned at all. I feel like the problem is somewhere around this line:

(($categoryCounter) ? '<span class="cnt">(' . $categoryCounter . ')</span>' : '') .

I tried changing it to $categoryCounter[$key] or something but that didn't work. Unfortunately my PHP skills are not good enough to find out what I'm doing wrong. If anyone can see what I'm obviously doing wrong I'd greatly appreciate some help :)

Here is the complete code;

while ($cat_details = $db->fetch_array($sql_select_categories)) 
{
    $categoryCounter = (COUNT_CATS == 1 && !empty($src_details['keywords_search'])) ? $cat_counter[$cat_details['category_id']] : $cat_details['items_counter'];
    if ($categoryCounter > 0 || COUNT_CATS == 0) {
        $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){

        $output .= '<tr> '.
        '   <td class="contentfont"><a href="' . $subcat_link . '">' . $category_lang[$key] . '</a> '.
        (($categoryCounter) ? '<span class="cnt">(' . $categoryCounter . ')</span>' : '') .
    ' </td> '.
    '</tr> ';

    }
}
7
  • 2
    One part you do wrong is the way how you word the title of your question. Commented Aug 8, 2012 at 0:16
  • Try to echo the value of $categoryCounter before the for loop or the if statement, to see what value it holds? Do a simple debug run. Commented Aug 8, 2012 at 0:17
  • We can't help you if you don't tell us the problem. What exactly is going wrong? Are you getting an error message? Commented Aug 8, 2012 at 0:18
  • @hakre: sorry, i wasn't really sure how to word it. Commented Aug 8, 2012 at 0:18
  • i get no error message at all. the part of the $categoryCounter where it's supposed to display a number it just shows up nothing. Commented Aug 8, 2012 at 0:19

2 Answers 2

0

If $categoryCounter is "0", then the condition ($categoryCounter) will be false, so you with your posted code, an empty string will be outputted, instead of the <span tag you are expecting.

0

This can't be the entire code as a few things aren't defined. Why does COUNT_CATS not start with a $? What is the layout of the $cat_array object? Are there multiple columns or is "name" the only column?

First up, try adding the $ to the front of these:

$COUNT_CATS == 1
$COUNT_CATS == 0

From what I can tell from what you provided, $categoryCounter will be the highest value from your loop starting with:

while ($cat_details = $db->fetch_array($sql_select_categories)) 

But currently you may not ever set the $cat_array as $categoryCounter may never be anything other than 0, and COUNT_CATS may or may not be valid.

Also, the $cat_array object could be assigned without the ["name"] part, i.e.

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

This would then have $cat_array[id]=$category_lang[id], which would then be sorted fine via the asort function. $cat_array[id]["name"]=$category_lang[id] would be sorted differently as it is now a multi-dimensional array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.