为了账号安全,请及时绑定邮箱和手机立即绑定

多个条目 - PHP 中的计数值

多个条目 - PHP 中的计数值

PHP
慕田峪7331174 2022-05-27 16:38:31
谢谢朋友们的帮助和反馈。我知道我不擅长 PHP,但仍在尝试学习和使用它:D——我的表包含针对 evaid 作为 Open Close 或 In Process 的重复条目——使用下面的代码,我从 DB 中获得了每个条目的最后输入状态通过使用查询和 if 语句来显示数据的状态,但我也想获取它的计数。任何人都可以帮助我---例如---$sql = "SELECT * FROM (SELECT * FROM disagreements ORDER BY addeddate DESC) disagreements  GROUP BY evaid";$result = mysqli_query($conn, $sql);if (mysqli_num_rows($result) > 0) {     while($row = mysqli_fetch_assoc($result))  {   // Here with this query I got last entered status of each row against evaid – as 2 Open – 5 in Process and 10 Closed --- with below if statement – I can echo the rows with status but I want to have count of it that how many are open, in process or closed        if($row["status"]=='Open') {  // I want to count this value as 2                    echo "<tr>";            echo "<td>" . $row["evaid"]. "</td>";            echo "<td>" . $row["status"]. "</td>";            echo "</tr>";        }     }} else {    echo "Nothing to Display";}mysqli_close($conn);
查看完整描述

2 回答

?
Cats萌萌

TA贡献1805条经验 获得超9个赞

// Hii.. 你可以从你的 SQL 查询中得到一个计数,试试这个


$sql = "SELECT *, COUNT(ID) AS COUNT FROM (SELECT * FROM disagreements ORDER BY addeddate DESC) disagreements  GROUP BY evaid";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) 

    while($row = mysqli_fetch_assoc($result)) 

    {   

        if($row["status"]=='Open')   

        {

           echo "<tr>";

           echo "<td>" . $row['count'] . "</td>"; // here you will get a count

           echo "<td>" . $row["evaid"]. "</td>";

           echo "<td>" . $row["status"]. "</td>";

           echo "</tr>";

        }

      } 

    } 

 else 

{

    echo "Nothing to Display";

}

mysqli_close($conn);


查看完整回答
反对 回复 2022-05-27
?
阿晨1998

TA贡献2037条经验 获得超6个赞

<?php 


$count['open']    = 0;


$count['close']   = 0;


$count['process'] = 0;


while($row = mysqli_fetch_assoc($result))  {   


    if($row["status"]=='Open') 

    {         

        $count['open']++;

        echo "<tr>";

        echo "<td>" . $row["evaid"]. "</td>";

        echo "<td>" . $row["status"]. "</td>";

        echo "</tr>";

    } 

    if($row["status"]=='Close') 

    {  // I want to count this value as 2        

    $count['close']++;

    echo "<tr>";

    echo "<td>" . $row["evaid"]. "</td>";

    echo "<td>" . $row["status"]. "</td>";

    echo "</tr>";

    } 

if($row["status"]=='Process') 

{  // I want to count this value as 2        

    $count['process']++;

    echo "<tr>";

    echo "<td>" . $row["evaid"]. "</td>";

    echo "<td>" . $row["status"]. "</td>";

    echo "</tr>";

}


}



print_r($count);


?>


查看完整回答
反对 回复 2022-05-27
  • 2 回答
  • 0 关注
  • 118 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号