Jump to content

[SOLVED] trying to total values in an array (from a query)


simon551

Recommended Posts

<?php

$total_cash = 0;

while($row_cash = mysql_fetch_array($rsERDets_cash)) {
	$total_cash = $total_cash + $row_cash['amtUS'];
	}

echo $total_cash;

?>

 

These links are useful:

 

http://uk.php.net/manual/en/language.expressions.php

http://uk.php.net/manual/en/language.operators.increment.php

  • 2 weeks later...

try this:

<?php

$total_cash = 0;

while($row_cash = mysql_fetch_array($rsERDets_cash)) {
	echo $row_cash['amtUS'] . '<br />';
	$total_cash = $total_cash + $row_cash['amtUS'];
	}

echo $total_cash;

?>

and see what it outputs. If you get nothing from the echo I've added, then there's something wrong with your mysql.. maybe you're selecting the wrong column, or have spelt it incorrectly

This is the query header

$rsERDets_cash = mysql_query($query_rsERDets_cash, $conn_org) or die(mysql_error());
$row_rsERDets_cash = mysql_fetch_assoc($rsERDets_cash);
$totalRows_rsERDets_cash = mysql_num_rows($rsERDets_cash);
$row_cash = mysql_fetch_array($rsERDets_cash);

 

This outputs 2 lines: one is $300 and another is $125:

	<?php
//start new Show if recordset not empty 
 if ($totalRows_rsERDets_cash > 0) 
 { 
	 ?>
  <tr>
    <td> </td>
    <td colspan="2"><h5>Cash Expenses to be reimbursed: </h5></td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
<?php
 //start a repeat region
 //set-up color alteration
  $i = 0;
  do {
  $i++;
  ?>
  <?php print "<tr class=\"d".($i & 1)."\">";?>
    <td><?php echo $row_rsERDets_cash['fdate']; ?></td>
    <td><?php echo $row_rsERDets_cash['ItemName']; ?></td>
    <td><?php echo $row_rsERDets_cash['vendor']; ?></td>
    <td><?php echo $row_rsERDets_cash['description']; ?></td>
    <td><?php echo $row_rsERDets_cash['internalNote']; ?></td>
    <td><?php echo $row_rsERDets_cash['Project']; ?></td>
    <td><?php echo $row_rsERDets_cash['amtForeign']; ?></td>
    <td><?php echo $row_rsERDets_cash['amtUS']; ?></td>
    <td><?php echo $row_rsERDets_cash['PERS']; ?></td>
    <td><a href="erR_dets_edit.php?erDetID_url=<?php echo $row_rsERDets_cash['erDetID']; ?>">edit</a></td>
    <td><a href="erR_dets_split.php?erDetID_url=<?php echo $row_rsERDets_cash['erDetID']; ?>">split</a></td>
  </tr>
    <?php 
//end repeat
} while ($row_rsERDets_cash = mysql_fetch_assoc($rsERDets_cash));
?>
  </tr>

 

This prints a 0:

<?php

$total_cash = 0;

while($row_cash = mysql_fetch_array($rsERDets_cash)) {
	echo $row_cash['amtUS'] . '<br />';
	$total_cash = $total_cash + $row_cash['amtUS'];
	}

echo $total_cash;

?>

<?php

Post your query here.
------------------------------------------------------

Run the following code and post output
we need to check your query result.

$total_cash = 0;

while($totalRows_rsERDets_cash = mysql_fetch_array($rsERDets_cash)) {
	$total_cash = $total_cash + $row_cash['amtUS'];
            echo $total_cash;
                          echo "<br>";
                          echo $row_cash['amtUS'];
                         }

echo $total_cash;

?>

<?php require_once('../Connections/conn_org.php'); ?>
<?php

//expense report from url variable
$erurl = "-1";
if (isset($_GET['erurl'])) {
  $erurl = (get_magic_quotes_gpc()) ? $_GET['erurl'] : addslashes($_GET['erurl']);
}
//Query of Cash charges
mysql_select_db($database_conn_org, $conn_org);
$query_rsERDets_cash = ("SELECT expensereports.erID, expensereports.erDate, expensereports.empID, expensereports.notes, expensereport_details.erDetID, expensereport_details.erID, expensereport_details.fdate, expensereport_details.vendor, expensereport_details.payType, CONCAT(Left(tblemployees.EmpFName, 2) , Left(tblemployees.EmpLName, 4)) AS CardOf, expensereport_splits.erSplitID, tblitems.ItemName, expensereport_splits.description, CONCAT(tblclients.ClientName,' - ',tblproj.ProjName) AS Project, expensereport_splits.Curr, expensereport_splits.amtForeign, expensereport_splits.fxRate, expensereport_splits.amtUS, expensereport_splits.internalNote, IF(expensereport_splits.Personal=0,'No','Yes') AS PERS, expensereport_splits.Reimb FROM expensereport_details Inner Join expensereport_splits ON expensereport_details.erDetID = expensereport_splits.erDetID Inner Join expensereports ON expensereports.erID = expensereport_details.erID Inner Join tblemployees ON expensereport_details.empID_accountof = tblemployees.EmpID Inner Join tblitems ON expensereport_splits.ItemID = tblitems.ItemID Inner Join tblproj ON expensereport_splits.ProjID = tblproj.ProjID Inner Join tblclients ON tblproj.ClientID = tblclients.ClientID WHERE payType = 'Cash' AND expensereports.erID = $erurl");
$rsERDets_cash = mysql_query($query_rsERDets_cash, $conn_org) or die(mysql_error());
$row_rsERDets_cash = mysql_fetch_assoc($rsERDets_cash);
$totalRows_rsERDets_cash = mysql_num_rows($rsERDets_cash);
$row_cash = mysql_fetch_array($rsERDets_cash);
?>
<!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>
<?php
$total_cash = 0;

while($totalRows_rsERDets_cash = mysql_fetch_array($rsERDets_cash)) {
	$total_cash = $total_cash + $row_cash['amtUS'];
            echo $total_cash;
                          echo "<br>";
                          echo $row_cash['amtUS'];
                         }

echo $total_cash;
?>

</body>
</html>

 

output is:

30

3030

 

the query using navicat is returning 3 rows with 'amtUS' of 30, 100 and 325 respectively.

 

strange. I guess there is some problem with the query because in the page the result is showing just 2 lines: 100 and 325. arggh

well, the missing line seems to be part of the same problem. I took out the '$row_cash=' declaration at the top and am getting 2 lines where there should be 3, 1 line where there should be 2, 3 lines where there should be 4, etc. ever heard of that?

<?php require_once('../Connections/conn_org.php'); ?>
<?php

//expense report from url variable
$erurl = "-1";
if (isset($_GET['erurl'])) {
  $erurl = (get_magic_quotes_gpc()) ? $_GET['erurl'] : addslashes($_GET['erurl']);
}
//Query of Cash charges
mysql_select_db($database_conn_org, $conn_org);
$query_rsERDets_cash = ("SELECT expensereports.erID, expensereports.erDate, expensereports.empID, expensereports.notes, expensereport_details.erDetID, expensereport_details.erID, expensereport_details.fdate, expensereport_details.vendor, expensereport_details.payType, expensereport_splits.erSplitID, tblitems.ItemName, expensereport_splits.description, CONCAT(tblclients.ClientName,' - ',tblproj.ProjName) AS Project, expensereport_splits.Curr, expensereport_splits.amtForeign, expensereport_splits.fxRate, expensereport_splits.amtUS, expensereport_splits.internalNote, IF(expensereport_splits.Personal=0,'No','Yes') AS PERS, expensereport_splits.Reimb FROM expensereport_details Inner Join expensereport_splits ON expensereport_details.erDetID = expensereport_splits.erDetID Inner Join expensereports ON expensereports.erID = expensereport_details.erID Inner Join tblitems ON expensereport_splits.ItemID = tblitems.ItemID Inner Join tblproj ON expensereport_splits.ProjID = tblproj.ProjID Inner Join tblclients ON tblproj.ClientID = tblclients.ClientID WHERE payType = 'Amex' AND expensereports.erID = $erurl");
$rsERDets_cash = mysql_query($query_rsERDets_cash, $conn_org) or die(mysql_error());
$row_rsERDets_cash = mysql_fetch_assoc($rsERDets_cash);
$totalRows_rsERDets_cash = mysql_num_rows($rsERDets_cash);
?>
<!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>
<?php

$total_cash = 0;
do {	$total_cash = $total_cash + $row_cash['amtUS'];

while($row_cash = mysql_fetch_array($rsERDets_cash)) {
	echo $row_cash['amtUS'] . '<br />';
	$total_cash = $total_cash + $row_cash['amtUS'];
	}

echo $total_cash;

?>
</body>
</html>

this works:

<p>
the rows of output:
</p>
<?php 
do{
echo $row_rsERDets_cash['amtUS'] . '<br />';
$total_cash += $row_rsERDets_cash['amtUS'];
}while ($row_rsERDets_cash = mysql_fetch_assoc($rsERDets_cash));
?>
<p>
the totalling:
</p>
<?php 
echo $total_cash;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.