我想知道某个类别中有多少产品。所以我将包含产品的表循环到类别表中,并查找了如何计算数组,但我无法使其工作。
目前我有以下代码:
// Productcategorieen
$pcat = "SELECT * FROM `snm_categories` WHERE parent_id = $pid and published = 1";
$pcatcon = $conn->query($pcat);
while ($pcat = $pcatcon->fetch_assoc()){
if($pcat['id'] != ''){
// Aantal producten binnen een categorie
$aantal = "SELECT * FROM `snm_content` WHERE catid = ".$pcat['id']." and state = 1";
$aantalcon = $conn->query($aantal);
while ($aantal = $aantalcon->fetch_assoc()){
$count = array_count_values($aantal);
$result = $count['id'];
echo '<pre>';
print_r($aantal);
echo '</pre>';
}
echo $result;
$productcatoverzicht .= '
<li class="cat-item"><a href="&'.$pcat['alias'].'">'.$pcat['title'].'</a><span class="count">(9)</span>
</li>';
}
}
echo $productcatoverzicht;
但当我回复$result
时,我收到以下消息:
Warning: array_count_values() expects parameter 1 to be array, null given in /home/website/public_html/_extern/website/catalogus.php on line 452
当我print_r($aantalvar)
时,这是输出(不是全部,而是一个条目):
Array
(
[0] => 5
[id] => 5
[1] => 79
[asset_id] => 79
[2] => Product 1
[title] => Product 1
[3] => product-1
[alias] => product-1
[4] =>
[introtext] =>
[5] =>
[fulltext] =>
[6] => 1
[state] => 1
[7] => 18
[catid] => 18
[8] => 2017-08-01 08:15:33
[created] => 2017-08-01 08:15:33
[9] => 360
[created_by] => 360
[10] =>
[created_by_alias] =>
[11] => 2017-08-01 08:15:33
[modified] => 2017-08-01 08:15:33
[12] => 0
[modified_by] => 0
[13] => 0
[checked_out] => 0
[14] => 0000-00-00 00:00:00
[checked_out_time] => 0000-00-00 00:00:00
[15] => 2017-08-01 08:15:33
[publish_up] => 2017-08-01 08:15:33
[16] => 0000-00-00 00:00:00
[publish_down] => 0000-00-00 00:00:00
[17] => {"image_intro":"","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
[images] => {"image_intro":"","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
[18] => {"urla":false,"urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""}
[urls] => {"urla":false,"urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""}
[19] => {"show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_layout":"","show_publishing_options":"","show_article_options":"","show_urls_images_backend":"","show_urls_images_frontend":""}
[attribs] => {"show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_layout":"","show_publishing_options":"","show_article_options":"","show_urls_images_backend":"","show_urls_images_frontend":""}
[20] => 1
[version] => 1
[21] => 0
[ordering] => 0
[22] =>
[metakey] =>
[23] =>
[metadesc] =>
[24] => 1
[access] => 1
[25] => 0
[hits] => 0
[26] => {"robots":"","author":"","rights":"","xreference":""}
[metadata] => {"robots":"","author":"","rights":"","xreference":""}
[27] => 0
[featured] => 0
[28] => *
[language] => *
[29] =>
[xreference] =>
)
如何计算整个数组中出现多少[id]?
一个问题:有多个来自$aantalvar
的数组,您可以在此处看到:
我需要知道[id]
在所有这些中出现了多少。
答案 0 :(得分:0)
你可以尝试一下。
// Productcategorieen
$pcat = "SELECT * FROM `snm_categories` WHERE parent_id = $pid and published = 1";
$pcatcon = $conn->query($pcat);
while ($pcat = $pcatcon->fetch_assoc()){
if($pcat['id'] != ''){
// Aantal producten binnen een categorie
$aantal = "SELECT * FROM `snm_content` WHERE catid = ".$pcat['id']." and state = 1";
$aantalcon = $conn->query($aantal);
$aantal = $aantalcon->fetch_assoc();
if(is_null($aantal)){
$total = 0;
}else{
$total = count($aantal);
}
$productcatoverzicht .= '
<li class="cat-item"><a href="&'.$pcat['alias'].'">'.$pcat['title'].'</a><span class="count">'.$total.'</span>
</li>';
}
}
echo $productcatoverzicht;