rowCount基于行值

时间:2016-02-19 16:07:24

标签: php mysql

所以我想这样做..

    $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>";
      }



  }

基本上,如果STAGE = 1行中的值我希望它计算这些行并给我数字..如果STAGE = 2的值我希望它计算这些行并给我数字。

现在,它只是计算所有的行..所以对于两个IF语句,它的计数数字是2,当每个部分只有1时..

1 个答案:

答案 0 :(得分:1)

我认为您需要执行三个不同的语句,一个用于获取所有行(用于循环并创建输出),另一个用于获取每个计数

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

您可以执行这些其他操作来获取您应该使用的计数,而不是循环中的$ count。

然后你的while循环变为

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>";
  }
}