gpt4 book ai didi

php - 检索包含字符串的行总数

转载 作者:行者123 更新时间:2023-11-30 22:56:00 24 4
gpt4 key购买 nike

我在 MySQL 中有一个表,在“类型”列中有 5(种)可能的值...我说是因为数据类型是“设置”并且 1 种类型有一个子类别...它是为了一种属性(property)类型,因此可能的类型是零售、办公、酒店、工业、住宅(多户家庭)、住宅(单户家庭)。

我正在尝试对结果进行分页,我需要知道每个结果应该有多少页。所以我需要一个查询来告诉我表中每种类型有多少,用户可以选择住宅作为一个类别,或者单一、多个作为子类别。

我不知道如何执行一个查询来告诉我每个有多少,或者如何检索这些数字作为变量,我可以用它来划分每页的项目。

id | type
-----------------------
1 | office
2 | residential,single
3 | industrial
4 | residential,multi
5 | retail
6 | office
7 | hospitality
8 | residential,single

等....

所以如果这是数据,我需要得到:

$office = 2  
$residential = 3
$industrial = 1
$single = 2

等...

最佳答案

使用array_count_values()函数

检查链接 http://php.net/manual/en/function.array-count-values.php

来自他们的网站 http://php.net/manual/en/function.array-count-values.php ;

并尝试这段代码

<?php

$query= // Run your select query.
$result= mysqli_query($link, $query);
//Run the while loop
while($row= mysqli_fetch_array($result))
{
$array[]=$row['Column_Name'];//Store the result in array
}

$count = array_count_values($array);//Use array count

print_r($count);//See the result

或者,如果你看到 The out put the way you want

$count 数组上运行 Foreach 循环

foreach($count as $key => $value) {
//Get the out put From new array
echo $value .' '. $key.'<br/>' ;
}

关于php - 检索包含字符串的行总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26351388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com