jonnyfortis Posted May 9, 2013 Share Posted May 9, 2013 I have a set rental amount with some variable making an equation. The final variable is the TOTAL of all the equations, i need the total of this variable <?php //option 1 52 weeks $weeklyToStudent = $row_rsTenProp['rental_price']; $fourWeeksSecurityDep = 4 * $weeklyToStudent; $fivetwoWeeksPayable = $weeklyToStudent * 52; $fee = 184.80; $resFee = 250; $fivetwoPayIncFeedSecDep = $fourWeeksSecurityDep + $fivetwoWeeksPayable + $fee; $initPay15Wks = $weeklyToStudent*15; $initPay15WksFeeSecDep = $initPay15Wks + $fee + $fourWeeksSecurityDep; $balanceB4MovingIn = $initPay15WksFeeSecDep - $resFee; $second14Wks = $weeklyToStudent * 14; $third14Wks = $weeklyToStudent * 14; $fourth9Wks = $weeklyToStudent * 9; $totalPayment = $initPay15WksFeeSecDep + $second14Wks + $third14Wks + $fourth9Wks; ?> <?php do { ?> <tr> <td class="table-text"><a href="customer-info.php?recordID=<?php echo $row_rsTenProp['userid']; ?>"><?php echo $row_rsTenProp['userid']; ?></a></td> <td class="table-text"><?php echo $row_rsTenProp['weeks'] / 7; ?> weeks</td> <td class="table-text"><?php echo $row_rsTenProp['payment_option']; ?></td> <td class="table-text"><?php echo $row_rsTenProp['rental_price']; ?></td> <td class="table-text"><?php echo $row_rsTenProp['prop_id']; ?></td> <td class="table-text"><?php echo DoFormatCurrency($totalPayment, 2, ',', '.', '£ '); ?></td> <td> </td> <td> </td> </tr> <?php } while ($row_rsTenProp = mysql_fetch_assoc($rsTenProp)); ?> i need to multiple the $totalPayment to get the TOTAL of this variable <?php $totalPaymentTot = 0; while ($totalPayment = ($row_totalPayment)); $totalPaymentTot += $row_totalPayment;{ $row_totalPayment; } echo $totalPaymentTot;?> Link to comment https://forums.phpfreaks.com/topic/277827-multiple-a-single-variable/ Share on other sites More sharing options...
Jessica Posted May 10, 2013 Share Posted May 10, 2013 And the problem is...? Link to comment https://forums.phpfreaks.com/topic/277827-multiple-a-single-variable/#findComment-1429422 Share on other sites More sharing options...
jonnyfortis Posted May 10, 2013 Author Share Posted May 10, 2013 And the problem is...? my code is returning the value of 0 and not the total Link to comment https://forums.phpfreaks.com/topic/277827-multiple-a-single-variable/#findComment-1429464 Share on other sites More sharing options...
davidannis Posted May 13, 2013 Share Posted May 13, 2013 ($totalPayment = ($row_totalPayment)) I think where $row_totalPayment comes from but if you want to see if it equals $totalPayment you need == not = I also can't figure out what you are doing with this { $row_totalPayment; } Link to comment https://forums.phpfreaks.com/topic/277827-multiple-a-single-variable/#findComment-1429852 Share on other sites More sharing options...
jonnyfortis Posted May 13, 2013 Author Share Posted May 13, 2013 ($totalPayment = ($row_totalPayment)) I think where $row_totalPayment comes from but if you want to see if it equals $totalPayment you need == not = I also can't figure out what you are doing with this { $row_totalPayment; } thanks my trouble. i need to create a loop and dont know how to do it. Link to comment https://forums.phpfreaks.com/topic/277827-multiple-a-single-variable/#findComment-1429859 Share on other sites More sharing options...
davidannis Posted May 13, 2013 Share Posted May 13, 2013 What are you trying to loop through? Looks like you are trying to read database records but I don't see where you open a database or select records. Do you open a database someplace else and you just didn't post that code? Link to comment https://forums.phpfreaks.com/topic/277827-multiple-a-single-variable/#findComment-1429916 Share on other sites More sharing options...
jonnyfortis Posted May 14, 2013 Author Share Posted May 14, 2013 What are you trying to loop through? Looks like you are trying to read database records but I don't see where you open a database or select records. Do you open a database someplace else and you just didn't post that code? hello the variable i am trying to multiply not coming from a database it is a variable resulting from other variable equations. the only variable coming from the DB is $weeklyToStudent = $row_rsTenProp['rental_price']; here is all the code from the page <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $colname_rsProperty = "-1"; if (isset($_GET['recordID'])) { $colname_rsProperty = $_GET['recordID']; } mysql_select_db($database_hostprop, $hostprop); $query_rsProperty = sprintf("SELECT * FROM host_editprop, host_editpropUtil, host_editpropUtilComp WHERE host_editprop.prop_id = %s AND host_editpropUtil.prop_id = host_editprop.prop_id AND host_editpropUtilComp.utilID = host_editpropUtil.UtilPropNameID", GetSQLValueString($colname_rsProperty, "text")); $rsProperty = mysql_query($query_rsProperty, $hostprop) or die(mysql_error()); $row_rsProperty = mysql_fetch_assoc($rsProperty); $totalRows_rsProperty = mysql_num_rows($rsProperty); mysql_select_db($database_hostprop, $hostprop); $query_rsTenants = sprintf("SELECT * FROM host_editprop, plus_signup WHERE host_editprop.prop_id = plus_signup.propFull AND plus_signup.propFull = %s", GetSQLValueString($colname_rsProperty, "text")); $rsTenants = mysql_query($query_rsTenants, $hostprop) or die(mysql_error()); $row_rsTenants = mysql_fetch_assoc($rsTenants); $totalRows_rsTenants = mysql_num_rows($rsTenants); mysql_select_db($database_hostprop, $hostprop); $query_rsTenProp = sprintf("SELECT * FROM host_editprop, plus_signup WHERE host_editprop.prop_id = plus_signup.prop_id AND plus_signup.propFull = %s", GetSQLValueString($colname_rsProperty, "text")); $rsTenProp = mysql_query($query_rsTenProp, $hostprop) or die(mysql_error()); $row_rsTenProp = mysql_fetch_assoc($rsTenProp); $totalRows_rsTenProp = mysql_num_rows($rsTenProp); mysql_select_db($database_hostprop, $hostprop); $query_rsSum = sprintf("SELECT host_editpropUtil.utilityAmount, host_editprop.prop_id, host_editpropUtil.prop_id FROM host_editpropUtil, host_editprop WHERE host_editpropUtil.prop_id = host_editprop.prop_id AND host_editprop.prop_id = %s", GetSQLValueString($colname_rsProperty, "text")); $rsSum = mysql_query($query_rsSum, $hostprop) or die(mysql_error()); $totalRows_rsSum = mysql_num_rows($rsSum); $grandTotal = 0; // use a loop here while ($row_rsSum = mysql_fetch_assoc($rsSum)) { $grandTotal += $row_rsSum['utilityAmount']; $row['utilityAmount'];// and display the contents of the row //$row['SUM(utilityAmount)'];// and display the contents of the row } function DoFormatCurrency($theObject,$NumDigitsAfterDecimal,$DecimalSeparator,$GroupDigits,$CurrencySymbol) { $currencyFormat=$CurrencySymbol.number_format($theObject,$NumDigitsAfterDecimal,$DecimalSeparator,$GroupDigits); return ($currencyFormat); } ?> and here is the ALL the code in the BODY <table width="945" border="1" cellspacing="0" cellpadding="0"> <tr> <td bgcolor="#B5EC2B" class="table-text"> </td> <td bgcolor="#B5EC2B" class="table-text"> </td> <td bgcolor="#B5EC2B" class="table-text"> </td> <td bgcolor="#B5EC2B" class="table-text"> </td> <td bgcolor="#B5EC2B" class="table-text"> </td> <td colspan="3" align="center" bgcolor="#B5EC2B" class="table-text"><div align="center">TOTALS FOR THE YEAR</div></td> </tr> <tr> <td bgcolor="#B5EC2B" class="table-text">Tenant Name</td> <td bgcolor="#B5EC2B" class="table-text">Duration</td> <td bgcolor="#B5EC2B" class="table-text"><p>Payment </p> <p>Option</p></td> <td bgcolor="#B5EC2B" class="table-text">Rent</td> <td bgcolor="#B5EC2B" class="table-text">Room</td> <td bgcolor="#B5EC2B" class="table-text">Total Rent Payment</td> <td bgcolor="#B5EC2B" class="table-text">£6 for Room</td> <td bgcolor="#B5EC2B" class="table-text">Utility for Room</td> </tr> <?php do { ?> <tr> <?php //option 1 52 weeks NEW ///////DO NOT TOUCH AS EQUATION WILL NOT WORK AS HAS TO BE IN LOOP////////////////// $weeklyToStudent = $row_rsTenProp['rental_price']; $fourWeeksSecurityDep = 4 * $weeklyToStudent; $fivetwoWeeksPayable = $weeklyToStudent * 52; $fee = 184.80; $resFee = 250; $fivetwoPayIncFeedSecDep = $fourWeeksSecurityDep + $fivetwoWeeksPayable + $fee; $initPay15Wks = $weeklyToStudent*15; $initPay15WksFeeSecDep = $initPay15Wks + $fee + $fourWeeksSecurityDep; $balanceB4MovingIn = $initPay15WksFeeSecDep - $resFee; $second14Wks = $weeklyToStudent * 14; $third14Wks = $weeklyToStudent * 14; $fourth9Wks = $weeklyToStudent * 9; $totalPayment = $initPay15WksFeeSecDep + $second14Wks + $third14Wks + $fourth9Wks; ?> <?php // host £6 equation for option 1 $utilTotal = $totalPayment; $LessFeeCol2 = $utilTotal - $fee; $securityDeposit = $row_rsTenProp['rental_price'] * 4; $lessSecurityDepsoit = $LessFeeCol2 - $securityDeposit; $weekCon = $row_rsTenProp['weeks'] / 7; $rentLess21 = $row_rsTenProp['rental_price'] - 21; $weekTerm = $rentLess21 * $weekCon; $lessFees = $fee - $fourWeeksSecurityDep - $weekTerm; $utilTotalLessFees = $utilTotal - $lessFees; $totalB4Divide = $lessSecurityDepsoit - $weekTerm; $sixMultiDura = 6 * $weekCon; $utility = $totalB4Divide - $sixMultiDura; ?> <td class="table-text"><a href="customer-info.php?recordID=<?php echo $row_rsTenProp['userid']; ?>"><?php echo $row_rsTenProp['userid']; ?></a></td> <td class="table-text"><?php echo $row_rsTenProp['weeks'] / 7; ?> weeks</td> <td class="table-text"><?php echo $row_rsTenProp['payment_option']; ?></td> <td class="table-text"><?php echo DoFormatCurrency($row_rsTenProp['rental_price'], 2, ',', '.', '£ '); ?></td> <td class="table-text"><?php echo $row_rsTenProp['prop_id']; ?></td> <td class="table-text"><?php echo DoFormatCurrency($totalPayment, 2, ',', '.', '£ '); ?></td> <td class="table-text"><?php echo DoFormatCurrency($sixMultiDura, 2, ',', '.', '£ '); ?></td> <td class="table-text"><?php echo DoFormatCurrency($utility, 2, ',', '.', '£ ', ''); ?></td> </tr> <?php } while ($row_rsTenProp = mysql_fetch_assoc($rsTenProp)); ?> <tr> <td class="table-text"> </td> <td class="table-text"> </td> <td class="table-text"> </td> <td class="table-text"> </td> <td class="table-text">Totals</td> <td class="table-text"><?php //$totalPaymentTot = 0; //while //($row_totalPayment = $totalPayment);{ //$totalPaymentTot += $row_totalPayment; //$totalPayment; //} //echo $totalPaymentTot; ?> <?php //$totalPaymentTot = 0; //while //($totalPayment = ($row_totalPayment)); //$totalPaymentTot += $row_totalPayment;{ //$row_totalPayment; //} //echo $totalPaymentTot; { $row_totalPayment = $totalPayment; $final = $row_totalPayment += $totalPayment; } echo $final;?></td> <td class="table-text"> </td> <td class="table-text"> </td> </tr> </table> Link to comment https://forums.phpfreaks.com/topic/277827-multiple-a-single-variable/#findComment-1429975 Share on other sites More sharing options...
davidannis Posted May 15, 2013 Share Posted May 15, 2013 If I am understanding you correctly you are trying to total the $totalPayment column. Try replacing this <?php } while ($row_rsTenProp = mysql_fetch_assoc($rsTenProp)); ?> with this: <?php $grandTotal+= $totalPayment; } while ($row_rsTenProp = mysql_fetch_assoc($rsTenProp)); ?> and this </tr> </table> with this: </tr> <tr><td>Grand Total:</td><td><?php echo $grandTotal;?></td></tr> </table> If that works you will want to format the number and make the columns look pretty. Link to comment https://forums.phpfreaks.com/topic/277827-multiple-a-single-variable/#findComment-1430092 Share on other sites More sharing options...
jonnyfortis Posted May 15, 2013 Author Share Posted May 15, 2013 If I am understanding you correctly you are trying to total the $totalPayment column. Try replacing this <?php } while ($row_rsTenProp = mysql_fetch_assoc($rsTenProp)); ?> with this: <?php $grandTotal+= $totalPayment; } while ($row_rsTenProp = mysql_fetch_assoc($rsTenProp)); ?> and this </tr> </table> with this: </tr> <tr><td>Grand Total:</td><td><?php echo $grandTotal;?></td></tr> </table> If that works you will want to format the number and make the columns look pretty. hello, thanks, this does show another figure but it isnt the total of all the $totalPayment that are echoed out. Are we using $grandTotal = 0; // use a loop here while ($row_rsSum = mysql_fetch_assoc($rsSum)) { $grandTotal += $row_rsSum['utilityAmount']; $row['utilityAmount'];// and display the contents of the row //$row['SUM(utilityAmount)'];// and display the contents of the row } as this is referencing the recordset mysql_select_db($database_hostprop, $hostprop); $query_rsSum = sprintf("SELECT host_editpropUtil.utilityAmount, host_editprop.prop_id, host_editpropUtil.prop_id FROM host_editpropUtil, host_editprop WHERE host_editpropUtil.prop_id = host_editprop.prop_id AND host_editprop.prop_id = %s", GetSQLValueString($colname_rsProperty, "text")); $rsSum = mysql_query($query_rsSum, $hostprop) or die(mysql_error()); $totalRows_rsSum = mysql_num_rows($rsSum); if so this i dont think is correct because all i need to the total of the variable $totalPayment hang on..i know why it isnt adding up. it is also adding the $grandTotal to the equation aswell... all i need it the total of the $totalPayment Link to comment https://forums.phpfreaks.com/topic/277827-multiple-a-single-variable/#findComment-1430170 Share on other sites More sharing options...
jonnyfortis Posted May 15, 2013 Author Share Posted May 15, 2013 hello, thanks, this does show another figure but it isnt the total of all the $totalPayment that are echoed out. Are we using $grandTotal = 0; // use a loop here while ($row_rsSum = mysql_fetch_assoc($rsSum)) { $grandTotal += $row_rsSum['utilityAmount']; $row['utilityAmount'];// and display the contents of the row //$row['SUM(utilityAmount)'];// and display the contents of the row } as this is referencing the recordset mysql_select_db($database_hostprop, $hostprop); $query_rsSum = sprintf("SELECT host_editpropUtil.utilityAmount, host_editprop.prop_id, host_editpropUtil.prop_id FROM host_editpropUtil, host_editprop WHERE host_editpropUtil.prop_id = host_editprop.prop_id AND host_editprop.prop_id = %s", GetSQLValueString($colname_rsProperty, "text")); $rsSum = mysql_query($query_rsSum, $hostprop) or die(mysql_error()); $totalRows_rsSum = mysql_num_rows($rsSum); if so this i dont think is correct because all i need to the total of the variable $totalPayment hang on..i know why it isnt adding up. it is also adding the $grandTotal to the equation aswell... all i need it the total of the $totalPayment to go into more detail the $grandTotal is the total of another column for another table <table width="945" border="1" align="left" cellpadding="0" cellspacing="0"> <tr> <td bgcolor="#B5EC2B" class="table-text">Utility</td> <td bgcolor="#B5EC2B" class="table-text">Date Paid</td> <td bgcolor="#B5EC2B" class="table-text">Start Date</td> <td bgcolor="#B5EC2B" class="table-text">End Date</td> <td bgcolor="#B5EC2B" class="table-text">Amount</td> <td bgcolor="#B5EC2B" class="table-text">Tenants</td> <td bgcolor="#B5EC2B"><span class="table-text">Delete</span></td> <td bgcolor="#B5EC2B"><span class="table-text">Edit</span></td> </tr> <?php do { ?> <tr> <td class="table-text"><a href="utility-breakdown.php?recordID=<?php echo $row_rsProperty['utilityID']; ?>"><?php echo $row_rsProperty['utilName']; ?></a></td> <td class="table-text"><?php echo $row_rsProperty['utilityDatePaid']; ?></td> <td class="table-text"><?php echo $row_rsProperty['utilitySD']; ?></td> <td class="table-text"><?php echo $row_rsProperty['utilityED']; ?></td> <td class="table-text"><?php echo DoFormatCurrency($row_rsProperty['utilityAmount'], 2, ',', '.', '£ '); ?></td> <td class="table-text"><?php echo $row_rsProperty['utilityStudAmount']; ?></td> <td class="table-text"><a href="delete-utility.php?utilityID=<?php echo $row_rsProperty['utilityID']; ?>">Delete </a></td> <td class="table-text"><a href="edit-utility.php?utilityID=<?php echo $row_rsProperty['utilityID']; ?>">Edit</a></td> </tr> <?php } while ($row_rsProperty = mysql_fetch_assoc($rsProperty)); ?> </table> so to reiterate you code you showed me is showing the total of $totalPayment and the total of $row_rsProperty['utilityAmount'] but i just need the total of $totalPayment Link to comment https://forums.phpfreaks.com/topic/277827-multiple-a-single-variable/#findComment-1430172 Share on other sites More sharing options...
mac_gyver Posted May 15, 2013 Share Posted May 15, 2013 i think you are making this harder than it is. if you are trying to sum value(s) that are present inside of your display loop, just initialize the total variable to zero before the start of the loop, add the value(s) to the total inside of the loop, and use the total after the end of the loop. specifically what values do you want to add up to make the grand total? Link to comment https://forums.phpfreaks.com/topic/277827-multiple-a-single-variable/#findComment-1430176 Share on other sites More sharing options...
jonnyfortis Posted May 15, 2013 Author Share Posted May 15, 2013 i have now fixed it so the issue by creating another variable so this worked for this $grandTotal1 = 0;//total for Total Rent Payment <?php $grandTotal1+= $totalPayment; } while ($row_rsTenProp = mysql_fetch_assoc($rsTenProp)); ?> <?php echo DoFormatCurrency($grandTotal1, 2, ',', '.', '£ ', '');?> this now give me just the total of $totalPayment I do have two other columns i need to make the totals of in the same way but i tried to replicate the above but didn't work i have two more variable $sixMultiDura and $utility that need totals i tried the following $grandTotal1 = 0;//total for Total Rent Payment $sixPoundTotal = 0;//total for £6 for Room <?php $grandTotal1+= $totalPayment; } while ($row_rsTenProp = mysql_fetch_assoc($rsTenProp)); { $sixPoundTotal+= $sixMultiDura; } while ($row_rsTenProp = mysql_fetch_assoc($rsTenProp)); ?> <td class="table-text"><?php echo DoFormatCurrency($grandTotal1, 2, ',', '.', '£ ', '');?></td> <td class="table-text"><?php echo DoFormatCurrency($sixPoundTotal, 2, ',', '.', '£ ', '');?></td> but this is giving me the correct results for $grandTotal1 but the $sixPoundTotal is showing the last record in the $sixMultiDura Link to comment https://forums.phpfreaks.com/topic/277827-multiple-a-single-variable/#findComment-1430179 Share on other sites More sharing options...
mac_gyver Posted May 15, 2013 Share Posted May 15, 2013 your overall code, based on what you did post, has queries inside of loops inside of other loops... that's a bad design because - 1) running queries inside of loops take a lot longer than running a single query once. you can probably write one query using JOINs and simply have one loop to do everything you are trying to do. 2) it takes more code, so it is harder for you to see what your code is doing. without seeing what your code really is, best guess why you only get the last value is you are initializing that variable inside of the loop where that code is running and it is being set to zero every pass through the loop. Link to comment https://forums.phpfreaks.com/topic/277827-multiple-a-single-variable/#findComment-1430202 Share on other sites More sharing options...
jonnyfortis Posted May 15, 2013 Author Share Posted May 15, 2013 your overall code, based on what you did post, has queries inside of loops inside of other loops... that's a bad design because - 1) running queries inside of loops take a lot longer than running a single query once. you can probably write one query using JOINs and simply have one loop to do everything you are trying to do. 2) it takes more code, so it is harder for you to see what your code is doing. without seeing what your code really is, best guess why you only get the last value is you are initializing that variable inside of the loop where that code is running and it is being set to zero every pass through the loop. yes i really need to understand what is going on and how to clearly write a loop. Is it where my curly brackets are going? Its would be better if i could write just one query to handle this whole process but i am at a loss on how to do this Link to comment https://forums.phpfreaks.com/topic/277827-multiple-a-single-variable/#findComment-1430210 Share on other sites More sharing options...
jonnyfortis Posted May 15, 2013 Author Share Posted May 15, 2013 Does this look acceptable?, it does work but want to make sure its allowed $grandTotal1+= $totalPayment; $sixPoundTotal+= $sixMultiDura; } while ($row_rsTenProp = mysql_fetch_assoc($rsTenProp)); Link to comment https://forums.phpfreaks.com/topic/277827-multiple-a-single-variable/#findComment-1430213 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.