我想计算某个类别中有多少产品。因此,我在类别表中循环包含产品的表,并查找如何计算数组,但我无法让它工作。
目前我有以下代码:
// 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;
但是当我 echo $result
时,我收到以下消息:
警告:array_count_values() 期望参数 1 为数组,在/home/website/public_html/_extern/website/catalogus.php 第 452 行给出 null
这是我执行 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]
出现。
最佳答案
你可以尝试一下。
// 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;
关于php - 计算 php 数组内的特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45433341/