473,378 Members | 1,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to calculate the sum of the values in a loop

3
MY QUESTION: HOW TO SUM TOTAL VALUE OF ALL ROWS?
MY CODE:
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $q="SELECT * FROM product ";
  3. $r=mysql_query($q);
  4. $num=mysql_numrows($r);
  5. ?>
  6. name,instock,unitprice,value
  7. <br>
  8. <?
  9. $i=0;
  10. while ($i < $num) {
  11. $name=mysql_result($r,$i,"name");
  12. $instock=mysql_result($r,$i,"instock");
  13. $price1=mysql_result($rsklad,$i,"price1");
  14. echo $name;
  15. echo "/";
  16. echo $instock;
  17. echo "/";
  18. echo $price1;
  19. echo "/";
  20. echo $value=$instock*$price1;
  21. echo "<br>";
  22. ++$i;
  23. ?>
MY QUESTION: HOW TO SUM TOTAL VALUE OF ALL ROWS?
Expand|Select|Wrap|Line Numbers
  1. $total = sum($value);
???
Sep 23 '07 #1
7 13754
pbmods
5,821 Expert 4TB
Heya, Dobsun. Welcome to TSDN!

Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

Please use CODE tags when posting source code:

[CODE=php]
PHP code goes here.
[/CODE]
Sep 23 '07 #2
dobsun
3
Actualy I don't have problem to sum rows in mysql, I could sum it like this :
Expand|Select|Wrap|Line Numbers
  1. $q1="SELECT  sum(productsum) as a FROM product ";
  2. $r1=mysql_query($q1);
  3. $a= mysql_fetch_array($r1); 
  4. echo $a['a'];
  5.  
My problem is to show the SUM of all variables $value, see 1st post;
One sugestion is to make as follow:
Expand|Select|Wrap|Line Numbers
  1.  $value[$i]=$instock*$price1;
But now I must calculate ... something lake this:
Expand|Select|Wrap|Line Numbers
  1. $total=$value[0]+$value[1]+.....+$value[$i]; 
How to calculate atomatic??
Sep 23 '07 #3
pbmods
5,821 Expert 4TB
Heya, Dobsun.

Alrightey. Changed thread title.

Try creating a variable to store the total:
Expand|Select|Wrap|Line Numbers
  1. $total = 0;
  2. while( ... )
  3. {
  4.     .
  5.     .
  6.     .
  7.     $total += $value;
  8. }
  9.  
Sep 23 '07 #4
dobsun
3
DONE thank you! correct one:
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $q="SELECT * FROM product ";
  3. $r=mysql_query($q);
  4. $num=mysql_numrows($r);
  5. ?>
  6. name,instock,unitprice,value
  7. <br>
  8. <?
  9. $i=0;
  10. while ($i < $num) {
  11. $name=mysql_result($r,$i,"name");
  12. $instock=mysql_result($r,$i,"instock");
  13. $price1=mysql_result($rsklad,$i,"price1");
  14. echo $name;
  15. echo "/";
  16. echo $instock;
  17. echo "/";
  18. echo $price1;
  19. echo "/";
  20. echo $value=$instock*$price1;
  21. echo "<br>";
  22. $total=$value+$temp;
  23. ++$i;
  24. $temp=$total;
  25. ?>
Work for me!
Sep 23 '07 #5
pbmods
5,821 Expert 4TB
Heya, Dobsun.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Sep 23 '07 #6
wow just shows me how I take simple algorithms for granted...
Sep 24 '07 #7
$tot=0;

for($i=1;$i<=10;$i++)
{

$tot=$tot+$i;

}
Sep 27 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: David | last post by:
I am learning plsql. I would like to run a stored procedure to calculate my bank account value by predicted 10% annual growth rate. Below is my plsql that is having problems. Your help is highly...
1
by: Building Blocks | last post by:
Hi, All I need is a simle calculate form script which contains this: A script that can handle text input, radio buttons, checkboxes, and dropdowns. Each one of these variables will contain a...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
1
by: Sam | last post by:
I successfully created DataGrid with connected environment but how do I perform sum of TotalRBAmount in DataGrid? Coding of ASP.Net 1.1 ------------------------ <form runat="server">...
96
by: david ullua | last post by:
I am reading "Joel on Software" these days, and am in stuck with the question of "how to calculate bitsize of a byte" which is listed as one of the basic interview questions in Joel's book. Anyone...
5
by: kux | last post by:
Hello everyone, I hope someone is out here who can help me with a simple calculation... I have a sales data base in access with monthly sales history by product. to make future predictions I...
4
by: Mayhem05 | last post by:
I'm hoping someone can guide me on solving this vexing problem I have with a database I built to track a projects. I'm using MS Access 2003 and here are the basics: the database is designed to...
3
by: coolguyraj | last post by:
I have a javascript code to take value from two text boxes and calculate on triggering the "OnBlur" function and display in the third box. The code works fine with one line item,If i have more...
0
by: SuzK | last post by:
I am trying to calculate in VBA in Access 2002 a moving average and update a table with the calculations. Fields in my WeeklyData table are Week Ending (date) ItemNbr (double) Sales Dollars...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.