Kmiles1990123
Kmiles1990123

Reputation: 189

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

Upvotes: 0

Views: 41

Answers (1)

Robin
Robin

Reputation: 2616

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

Upvotes: 1

Related Questions