如何通过html表显示多个数据库值的数值

k2fxgqgv  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(337)

我有一个页面,希望在其中显示数据库中值的“多少”。我附上了一张我目前所拥有的照片。根据我目前的统计,我知道我有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>";
            }
          }
    ?>
nlejzf6q

nlejzf6q1#

为您的帐户使用别名 COUNT(*) ,即 resourceCount ```
SELECT username, primary_function, COUNT(*) as resourceCount

然后用 `primary_function` 以及 `resourceCount` ```
$resourceCounts = array();
while ($rowCounts = mysqli_fetch_assoc($countresult)) {
    $resourceCounts['primary_function'] = $rowCounts['resourceCount'];
}

然后在你的table上,用 $row['primary_function'] 为了得到 resourceCount -> $resourceCounts[$row['primary_function']] ```
echo "". $row['pf_id'] ."". $row['primary_function'] ."". $resourceCounts[$row['primary_function']] ."";

要得到最终的总数,请创建一个计数器var并将该值添加到其中-

$total = 0;
....
// add the value in the loop
$total += $resourceCounts[$row['primary_function']];
....

相关问题