Presto-X Posted January 15, 2007 Share Posted January 15, 2007 How would I go about adding up values using While? In the past I used somthing like this [code]$paysubtotal = $total - $payment_amount;[/code] But now that I'm starting learn a bit more about php I want to pull a list of payments from my database, right now it will total up the first amount on the list.This is how I have it all setup first I have a total, lets say $200 then I have a form to submit payments to the database. The payments should subtracked from the total amount. It works if I only have one payment but if I add any more they do not get subtracked.This is the code that I came up with:[code]$query_payment="SELECT id, payment_id, payment_amount, payment_date FROM customer_payments";$payment_result=mysql_query($query_payment);echo "SUBTOTAL: $$total<br />PAYMENTS:<br />";$payment_num=mysql_numrows($payment_result);$payment=0;while ($payment < $payment_num) { $paymentid=mysql_result($payment_result,$payment,"id"); $payment_id=mysql_result($payment_result,$payment,"payment_id"); $payment_amount=mysql_result($payment_result,$payment,"payment_amount"); $payment_date=mysql_result($payment_result,$payment,"payment_date"); if ( $payment_id == "$id" ) { echo"$$payment_amount - $payment_date<br />"; } $payment++;}$paysubtotal = $total - $payment_amount;echo "$paysubtotal";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/34293-math-adding-up-values-when-using-while/ Share on other sites More sharing options...
sasa Posted January 15, 2007 Share Posted January 15, 2007 move line[code]$paysubtotal = $total - $payment_amount;[/code] in if part[code]if ( $payment_id == "$id" ) { echo"$$payment_amount - $payment_date<br />"; $paysubtotal = $total - $payment_amount; }[/code]btw you can add WHERE part in your SQL[code]$query_payment="SELECT id, payment_id, payment_amount, payment_date FROM customer_payments WHERE payment_id='$id'";[/code]and you don't need if statemans at all Quote Link to comment https://forums.phpfreaks.com/topic/34293-math-adding-up-values-when-using-while/#findComment-161494 Share on other sites More sharing options...
Presto-X Posted January 16, 2007 Author Share Posted January 16, 2007 Thanks for the info sasa,Ok this is the code that I'm using but it does not seem to be working...[code]<?PHPinclude '../include/config.php';include '../services/price-list.php';mysql_connect($server, $username, $password);@mysql_select_db($database);// Query Payments$query_payment="SELECT id, payment_id, payment_amount, payment_date FROM customer_payments WHERE payment_id='$id'";$payment_result=mysql_query($query_payment);// Query Notes$query_notes="SELECT id, note_id, note_title, note_content, note_date FROM customer_notes";$notes_result=mysql_query($query_notes);$space=" ";$id = $_GET['id'];// Query Customers$result = mysql_query("SELECT company, firstname, lastname, address, city, state, zip, country, email, website, phone1, phone2, phone3, fax1, fax2, fax3, total, totaltime, description, domainreg, websitehosting, identitypack, graphicdesign, websitedesign, contentwriting, searchengine, ecommerce, payment FROM customers WHERE id = '$id'");mysql_close();$i = mysql_fetch_row($result);// Contact Information$company = $i[0];$firstname = $i[1];$lastname = $i[2];$address = $i[3];$city = $i[4];$state = $i[5];$zip = $i[6];$country = $i[7];$email = $i[8];$website = $i[9];$phone1 = $i[10];$phone2 = $i[11];$phone3 = $i[12];$fax1 = $i[13];$fax2 = $i[14];$fax3 = $i[15];// Billing Information$total = $i[16];$totaltime = $i[17];$description = $i[18];$payment = $i[27];// Pricing Information$domainreg = $i[19];$websitehosting = $i[20];$identitypack = $i[21];$graphicdesign = $i[22];$websitedesign = $i[23];$contentwriting = $i[24];$searchengine = $i[25];$ecommerce = $i[26];?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title></head><body><?PHPecho "SUBTOTAL: $$total<br />PAYMENTS:<br />";$payment_num=mysql_numrows($payment_result);$payment=0;while ($payment < $payment_num) { $paymentid=mysql_result($payment_result,$payment,"id"); $payment_id=mysql_result($payment_result,$payment,"payment_id"); $payment_amount=mysql_result($payment_result,$payment,"payment_amount"); $payment_date=mysql_result($payment_result,$payment,"payment_date"); echo"$$payment_amount - $payment_date<br />"; $paysubtotal = $total - $payment_amount; $payment++;}echo "$paysubtotal";?></body></html>[/code]all the page shows is this:[code]SUBTOTAL: $250.00PAYMENTS:[/code] Quote Link to comment https://forums.phpfreaks.com/topic/34293-math-adding-up-values-when-using-while/#findComment-161628 Share on other sites More sharing options...
sasa Posted January 16, 2007 Share Posted January 16, 2007 OK 1st you use variable $id before yuo set up it in 1st query ($query_payment)It cause thet query returns no rows and $payment_num is zero and you never go in while loopline $id = $_GET['id']; move before line $query_payment = ...2nd Before while loop $paysubtotal = $total (not payment yet)In while loop for each paymant you must subtract payment_amount from paysubtotal ($paysubtotal = $paysubtotal - $payment_amount;)insert line $paysubtotal = $total; before while loopchange line $paysubtotal = $total - $payment_amount; in $paysubtotal = $paysubtotal - $payment_amount; Quote Link to comment https://forums.phpfreaks.com/topic/34293-math-adding-up-values-when-using-while/#findComment-162298 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.