我有一个页面,希望在其中显示数据库中值的“多少”。我附上了一张我目前所拥有的照片。根据我目前的统计,我知道我有13个运输项目,2个通信项目等,所以我想知道如何正确输出。
<?php
$countSQL = "SELECT username, primary_function, COUNT(*)
FROM resource
WHERE primary_function
IN ('transportation', 'communications', 'engineering', 'search and rescue', 'education', 'energy', 'firefighting', 'human services')
GROUP BY username, primary_function";
$countresult = mysqli_query($conn, $countSQL);
$countdata= mysqli_num_rows($countresult);
$sql = "SELECT pf_id, primary_function FROM primary_function ORDER BY pf_id;";
$result = mysqli_query($conn, $sql);
$queryResult = mysqli_num_rows($result);
if ($queryResult > 0){
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>". $row['pf_id'] ."</td><td>". $row['primary_function'] ."</td><td>". $countdata."</td></td>";
}
}
?>
1条答案
按热度按时间nlejzf6q1#
为您的帐户使用别名
COUNT(*)
,即resourceCount
```SELECT username, primary_function, COUNT(*) as resourceCount
然后在你的table上,用
$row['primary_function']
为了得到resourceCount
->$resourceCounts[$row['primary_function']]
```echo "". $row['pf_id'] ."". $row['primary_function'] ."". $resourceCounts[$row['primary_function']] ."";
$total = 0;
....
// add the value in the loop
$total += $resourceCounts[$row['primary_function']];
....