Counting With a For loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GlennCurtis2009
Forum Newbie
Posts: 11
Joined: Wed Jul 08, 2009 7:43 am

Counting With a For loop

Post by GlennCurtis2009 »

Hi guys,

I now have a working for loop that prints / re-prints data from one form into this one. What iam trying to do now is count how much the total is within the $From_Pr array (this array contains prices). $Used is set by the user on the 1st php page which gets passed but this form.

For example, if the user enters 4, then there will be four different prices they would have entered into the form, this will now print back but i also want to display the total these four prices will add up to.

Thanks Glenn Curtis.

Code: Select all

 
 
 
for ($i=1; $i<=$Used; $i++) {
 
    print ("<input type=\"text\" name=\"Form_In[$i]a\" class=\"Invoice_Items\" value=\"$Form_In[$i]\" />\n");
    print ("<input type=\"text\" name=\"Form_Pr[$i]a\" class=\"Invoice_Prices\" value=\"&pound; $Form_Pr[$i]\" /><br>\n");
 
//print ("<br><br><br>$Form_Pr[$i]");
$Total = $Form_Pr[$i] + $Form_Pr[$i];
 
 
}
 
print ("Total = $Total");
 
 
 
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Counting With a For loop

Post by Eric! »

Change line 10 to

Code: Select all

$Total = $Total + $Form_Pr[$i];
or in short hand

Code: Select all

$Total += $Form_Pr[$i];
Add a $Total=0; before the for loop too...
GlennCurtis2009
Forum Newbie
Posts: 11
Joined: Wed Jul 08, 2009 7:43 am

Re: Counting With a For loop

Post by GlennCurtis2009 »

Hi,

Thank. Working!!!

I did do that and thought that it should have worked but i forgot to zero total before the loop, but its now all working thank you.



Glenn Curtis.
Post Reply