simon551 Posted May 23, 2007 Share Posted May 23, 2007 Hi, shot in the dark here. But this doesn't work: <?php $total_cash = 0; while($row_cash = mysql_fetch_array($rsERDets_cash)) { $subtotal_cash = $row_cash['amtUS']; $total_cash += $subtotal_cash['amtUS']; echo $total_cash; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/ Share on other sites More sharing options...
chigley Posted May 23, 2007 Share Posted May 23, 2007 Why $subtotal_cash['amtUS'] ~ should it not just be $subtotal_cash? Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/#findComment-260189 Share on other sites More sharing options...
simon551 Posted May 23, 2007 Author Share Posted May 23, 2007 Thanks. Makes sense. Still not working though. -s Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/#findComment-260196 Share on other sites More sharing options...
chigley Posted May 23, 2007 Share Posted May 23, 2007 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/#findComment-260201 Share on other sites More sharing options...
simon551 Posted May 23, 2007 Author Share Posted May 23, 2007 still not totalling... Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/#findComment-260331 Share on other sites More sharing options...
simon551 Posted June 5, 2007 Author Share Posted June 5, 2007 can anyone help me figure out why this isn't working? Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/#findComment-268820 Share on other sites More sharing options...
Dragen Posted June 5, 2007 Share Posted June 5, 2007 are you getting anything for the total? Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/#findComment-268823 Share on other sites More sharing options...
simon551 Posted June 5, 2007 Author Share Posted June 5, 2007 yeah. 0 when the rows add to, say 500. Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/#findComment-268824 Share on other sites More sharing options...
trq Posted June 5, 2007 Share Posted June 5, 2007 can anyone help me figure out why this isn't working? Define not working. Were not mind readers! Maybe a read of the link in my signature (How to ask...) would help. Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/#findComment-268825 Share on other sites More sharing options...
Dragen Posted June 5, 2007 Share Posted June 5, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/#findComment-268827 Share on other sites More sharing options...
simon551 Posted June 5, 2007 Author Share Posted June 5, 2007 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/#findComment-268837 Share on other sites More sharing options...
Dragen Posted June 5, 2007 Share Posted June 5, 2007 I think you should be using this: <?php $total_cash = 0; while($totalRows_rsERDets_cash = mysql_fetch_array($rsERDets_cash)) { $total_cash = $total_cash + $row_cash['amtUS']; } echo $total_cash; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/#findComment-268840 Share on other sites More sharing options...
simon551 Posted June 6, 2007 Author Share Posted June 6, 2007 still 0 ... ?? Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/#findComment-268966 Share on other sites More sharing options...
jitesh Posted June 6, 2007 Share Posted June 6, 2007 <?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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/#findComment-268968 Share on other sites More sharing options...
simon551 Posted June 6, 2007 Author Share Posted June 6, 2007 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/#findComment-268974 Share on other sites More sharing options...
simon551 Posted June 6, 2007 Author Share Posted June 6, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/#findComment-268990 Share on other sites More sharing options...
simon551 Posted June 6, 2007 Author Share Posted June 6, 2007 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52696-solved-trying-to-total-values-in-an-array-from-a-query/#findComment-269235 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.