简体   繁体   中英

rowCount based on row value

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..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM