1

For example I have a mysql_num_rows results of 4,8,15,16,23,42 in a query that is inside a while loop of another query. My question is how can I total all the results inside that while loop? (Total of 133) Thanks.

EDIT:

How about if I want to get the percentage per each result of mysql_num_rows inside my while loop? Example: 4,8,15,16,23,42. Total is 108. $sum = 108. Percentage of 4 = 4/$sum = 3.7%, 8 = 8/$sum = 7.4% and so on..

4
  • 1
    Add them in a variable declared outside the while loop? $total = 0; while ($stuff) {$total = $total + mysql_num_rows();}... Commented Oct 17, 2011 at 2:51
  • Can we see the code? What about a variable outside the main loop initialized to zero, then add the mysql_num_rows result inside the while loop? Commented Oct 17, 2011 at 2:52
  • +1 for using the numbers popular in the TV Series Lost. :) Commented Oct 17, 2011 at 2:53
  • @Nathan LOL actually I was wrong, there's no number 12 in 'The Numbers'. Commented Oct 17, 2011 at 2:56

1 Answer 1

1

Try something like this:

$Sum = 0;
while ($SomeInvariant)
{
   mysql_query($SomeQuery);
   $Sum += mysql_num_rows();
}

echo 'The sum is: ' . $Sum;

However, this approach is not very efficient (what if $SomeInvariant is true for many iterations, and your app has even more concurrent users?). To account for this, I would recommend restructuring your approach so the addition is done in SQL. This way, your query could look something like this: SELECT SUM(ColumnName) FROM ....

UPDATE: Addressing follow-up question in the comments

If you don't already have the sum available from the query, then you'll have to loop over the dataset twice. On the first pass, you'll calculate the sum. On the second pass, you'll calculate the ratio of each value to the sum.

For example:

$Sum = 0;
$Rows = array();
while ($SomeInvariant)
{
   mysql_query($SomeQuery);
   $Value = mysql_num_rows();
   $Rows[] = $Value; // Push the value onto the row array
   $Sum += $Value;   // Add the value to the cumulative sum
}

echo 'The sum is: ' . $Sum;

foreach ($Rows as $Row)
{
    echo $Row . '/' . $Sum . ' = ' . number_format($Row / $Sum) . '%';
}
Sign up to request clarification or add additional context in comments.

4 Comments

"$Sum += mysql_num_rows();" - Thank you very much sir. I didn't know '+=' is possible. LOL Thanks a lot
All PHP operators have an "operate an assign" form. For example, operator A x= B is equivalent to A = A x B, where x is any of the PHP operators (+, -, *, /, <<, >>, ., etc.)
How about if I want to get the percentage per each result of mysql_num_rows inside my while loop? Example: 4,8,15,16,23,42. Total is 108. $sum = 108. Percentage of 4 = 4/$sum = 3.7%, 8 = 8/$sum = 7.4% and so on..
Thanks Zach. But I got another problem. I have to put all the results of "foreach ($Rows as $Row)" inside the while loop that has a table. For each result of the foreach I have to put it inside the while loop in a <td>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.