oliverbj
oliverbj

Reputation: 6052

php - count total num rows inside while loop

I've got this piece of code:

  $i=0;
      $start_date = date("Y/m/d");
      $end_date = date('Y/m/d', strtotime($start_date . " -7 days"));



      while($days7=mysql_fetch_assoc($q)): 
          $next_date = strtotime($i--." days", strtotime($start_date));
          $date = date("Y/m/d",$next_date); 
      #Let's get the latest click combined from the latest 7 days
          $combined7=mysql_query("SELECT sum(value) FROM `xeon_stats_clicks` WHERE user='".$userdata['username']."' AND typ='4' AND data='$date' ORDER BY data DESC LIMIT 8") or die(mysql_error());              


      print mysql_num_rows($combined7);

      endwhile;  

I need to see how many rows that $combined7 is getting. Currently, I am using print mysql_num_rows($combined7); but that just prints out: 1 1 1 1 1 (The number '1' for each row)

How can I count the total number?

(P.S. $i have to be set to 0)

Upvotes: 4

Views: 43118

Answers (4)

UnholyRanger
UnholyRanger

Reputation: 1971

Here is your original query:

          $combined7=mysql_query("SELECT sum(value) FROM `xeon_stats_clicks` WHERE user='".$userdata['username']."' AND typ='4' AND data='$date' ORDER BY data DESC LIMIT 8")

By adding the COUNT command, it will count the amount of rows that were considered in the SUM:

SELECT SUM(value), COUNT(value) FROM...

Then, when you get the MYSQL_RESULT back, you need to fetch the data:

$data = mysql_fetch_array($combined7);

This will then have the following array:

Array(
    [0] = SUM
    [1] = COUNT
)

NOTICE: mysql_* has been deprecated. Please use mysqli_* or PDO instead

Upvotes: 1

Md. Sahadat Hossain
Md. Sahadat Hossain

Reputation: 3236

you should define a variable with value 0 before while. then you increment value of this variable inside the while. then you print this variable after end of while.

      $start_date = date("Y/m/d");
      $end_date = date('Y/m/d', strtotime($start_date . " -7 days"));
      $sn = 0;
      while($days7=mysql_fetch_assoc($q)): 
          $next_date = strtotime($i--." days", strtotime($start_date));
          $date = date("Y/m/d",$next_date); 
      #Let's get the latest click combined from the latest 7 days
          $combined7=mysql_query("SELECT sum(value) FROM `xeon_stats_clicks` WHERE user='".$userdata['username']."' AND typ='4' AND data='$date' ORDER BY data DESC LIMIT 8") or die(mysql_error());              


      $sn += mysql_num_rows($combined7);

      endwhile;
      print $sn;

Upvotes: 0

Kalpit
Kalpit

Reputation: 4936

i didn't get your question proper.. but i think you want to count total updated row

 $sum=0;
 while(){
 $sum += mysql_num_rows($combined7); //here it will add total upadted row in $sum...
 print $sum; // if you want to print every time total
 }
 print $sum; // if you want to print only one time total

Upvotes: 0

q0re
q0re

Reputation: 1359

Simple:

$counter = 0;
while(..) {
      $counter++; // or $counter = $counter + 1;
}

echo $counter;

define the variable outside the loop.

Upvotes: 22

Related Questions