dpl3350 2016-02-19 16:07
浏览 26
已采纳

rowCount基于行值

So I am trying to do this..

    $userID = $_SESSION['user_session'];
    $stmt = $this->db->prepare("SELECT * FROM orrs");
    $stmt->bindparam(":id", $userID);
    $stmt->execute();
    $count = $stmt->rowCount();


 echo   
"<div class='table-responsive'>
 <table class='table' border='1'>
     <tr class='head'>
     <h3>Snapshot</h3>
     <th>Se</th>
     <th>#s</th>
     <th>Ae</th>
     <th>Prt</th>
     <th>Pin</th>


     </tr>";


      while($userRows=$stmt->fetch(PDO::FETCH_ASSOC)) {


      if($userRows['stage'] == '1')
      {
      echo "<tr>";
      echo "<td>" . "Newn" . "</td>";
      echo "<td>" . $count . "</td>";
      echo "<td>" . $userRows['aow'] . "</td>";
      echo "<td>" . $userRows['pit'] . "</td>";
      echo "<td>" . $userRows['pgin'] . "</td>";


      }
      else if($userRows['stage'] == '2')
      {

      echo "<tr>";
      echo "<td>" . "Pendinn" . "</td>";
      echo "<td>" . $count . "</td>";
      echo "<td>" . $userRows['gfh'] . "</td>";
      echo "<td>" . $userRows['pt'] . "</td>";
      echo "<td>" . $userRows[trin'] . "</td>";
      }



  }

Basically, If the value in the row STAGE = 1 I want it to count those rows and give me the number.. If the value of STAGE = 2 I want it to count those rows and give me the number.

Right now, It is just counting all of the rows.. So for both of the IF statment its count number is saying 2, When there is only 1 in each section..

  • 写回答

1条回答 默认 最新

  • doujiao1984 2016-02-19 16:13
    关注

    I think you need to execute three different statements, one to get all the rows (for you to loop over and create your output) and one to get each of the counts

    //The current one
    $stmt = $this->db->prepare("SELECT * FROM orrs");
    
    //The get the count for stage '1'
    $stage_1_stmt = $this->db->prepare("SELECT * FROM orrs where STAGE = 1");
    $stage_1_count = $stage_1_stmt->rowCount();
    
    //The get the count for stage '2'
    $stage_2_stmt = $this->db->prepare("SELECT * FROM orrs where STAGE = 2");
    $stage_2_count = $stage_2_stmt->rowCount();
    

    You can execute these others to get the counts which you should use in place of $count in your loop.

    Your while loop then becomes

    while($userRows=$stmt->fetch(PDO::FETCH_ASSOC)) {
    
    
      if($userRows['stage'] == '1')
      {
      echo "<tr>";
      echo "<td>" . "Newn" . "</td>";
      echo "<td>" . $stage_1_count . "</td>";
      echo "<td>" . $userRows['aow'] . "</td>";
      echo "<td>" . $userRows['pit'] . "</td>";
      echo "<td>" . $userRows['pgin'] . "</td>";
    
    
      }
      else if($userRows['stage'] == '2')
      {
    
      echo "<tr>";
      echo "<td>" . "Pendinn" . "</td>";
      echo "<td>" . $stage_2_count . "</td>";
      echo "<td>" . $userRows['gfh'] . "</td>";
      echo "<td>" . $userRows['pt'] . "</td>";
      echo "<td>" . $userRows[trin'] . "</td>";
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大