multiple a single variable

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
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

multiple a single variable

Post by jonnyfortis »

i have a variable showing the total of other equation and i need to multiple this variable

Code: Select all

$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;
i need to add all the results of the $totalPayment to give me a total

i have tried this

Code: Select all

$final = $totalPayment += $totalPayment;
				
				echo $final;
but this is just adding the first line by itself
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: multiple a single variable

Post by litebearer »

did you echo each variable to ascertain they contain what you expect? Also why not $final = $totalPayment * 2 ?
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: multiple a single variable

Post by jonnyfortis »

did you echo each variable to ascertain they contain what you expect? Also why not $final = $totalPayment * 2 ?
because that will muliply the total by two and i dont need that, i need to add all the $totalPayment togeather
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: multiple a single variable

Post by litebearer »

$totalPayment will only contain its last value.
It sounds like you are 'looping' thru something hoping to create distinct totals -
From your posted code there are no other $totalPayment (s).

IF in fact you are doing a loop, then

Code: Select all

begin loop
  get values and assign to the variables
  total the variables
  $final = $final + $totalPayment;
end loop
should do the trick
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: multiple a single variable

Post by jonnyfortis »

litebearer wrote:begin loop
  get values and assign to the variables
  total the variables
  $final = $final + $totalPayment;
end loop
thanks this is very helpful but im a stuck on this, could you possible show me an example of what you are showing me?

thanks for you help
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: multiple a single variable

Post by litebearer »

where are you stucK? perhaps if you showed us more of your code we could be more specific
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: multiple a single variable

Post by jonnyfortis »

litebearer wrote:where are you stucK? perhaps if you showed us more of your code we could be more specific
im stuck on the loop part
i have included all the relevant code ( there are some parts commented out for my reference and code that may longer not be needed )

Code: Select all

<?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);
}

?>
<?
	session_start();
	if(!$_SESSION['loggedIn']) // If the user IS NOT logged in, forward them back to the login page
	{
		header("location:Login.html");
	}
?>
body

Code: Select all

<table width="945" border="1" cellspacing="0" cellpadding="0">
            <tr>
              <td bgcolor="#B5EC2B" class="table-text">&nbsp;</td>
              <td bgcolor="#B5EC2B" class="table-text">&nbsp;</td>
              <td bgcolor="#B5EC2B" class="table-text">&nbsp;</td>
              <td bgcolor="#B5EC2B" class="table-text">&nbsp;</td>
              <td bgcolor="#B5EC2B" class="table-text">&nbsp;</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//////////////////
$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">&nbsp;</td>
                <td class="table-text">&nbsp;</td>
                <td class="table-text">&nbsp;</td>
                <td class="table-text">&nbsp;</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">&nbsp;</td>
                <td class="table-text">&nbsp;</td>
              </tr>
              
          </table>
i hope this is clearer
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: multiple a single variable

Post by jonnyfortis »

i have manage to get one part working so that the $totalPayment is now giving a total for that column

this is done by

Code: Select all

$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

Code: Select all

$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
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: multiple a single variable

Post by jonnyfortis »

done it

<?php
$grandTotal1+= $totalPayment;
$sixPoundTotal+= $sixMultiDura;
$utilRoom+= $utility;
} while ($row_rsTenProp = mysql_fetch_assoc($rsTenProp));
?>
Post Reply