多个条目-PHP中的计数值

时间:2019-11-29 05:43:32

标签: php sql

感谢朋友们的帮助和反馈。我知道我对PHP不太满意,但仍尝试学习和使用它:D-我的表包含重复条目,以防打开或关闭过程中发生错误---使用下面的代码,我从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 个答案:

答案 0 :(得分:1)

<?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);

?>

答案 1 :(得分:1)

// 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);