Page 1 of 1

Counting With a For loop

Posted: Wed Jul 08, 2009 7:07 pm
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");
 
 
 

Re: Counting With a For loop

Posted: Wed Jul 08, 2009 8:28 pm
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...

Re: Counting With a For loop

Posted: Thu Jul 09, 2009 4:00 am
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.