0

我有一段代码应该抓取类别,按字母顺序对它们进行排序(它们有多种语言版本),并为它们中的每一个返回一个计数。问题是,由于某种原因,根本没有返回计数。我觉得问题出在这条线上:

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

我尝试将其更改为 $categoryCounter[$key] 或其他东西,但没有奏效。不幸的是,我的 PHP 技能不足以找出我做错了什么。如果有人能看到我显然做错了什么,我将非常感谢一些帮助:)

这是完整的代码;

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> ';

    }
}
4

2 回答 2

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.

于 2012-08-08T00:55:17.217 回答
0

如果$categoryCounter"0",则条件($categoryCounter)为假,因此您使用发布的代码,将输出一个空字符串,而不是<span您期望的标签。

于 2012-08-08T00:33:09.287 回答