flemingmike Posted September 28, 2010 Share Posted September 28, 2010 hello, how would i add the values off all results in the $pamount array? $result2 = mysql_query("SELECT * FROM pobook WHERE jobnumber = '$jobid'"); while($row2 = mysql_fetch_array($result2)) { $pamount=$row2['amount']; echo "<tr>"; echo "<td>" . $pamount . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>VALUE OF ALL RETURNED $pamount HERE</td>"; echo "</tr>"; Quote Link to comment https://forums.phpfreaks.com/topic/214571-adding-values-of-array-together/ Share on other sites More sharing options...
flemingmike Posted September 28, 2010 Author Share Posted September 28, 2010 is this easily done? Quote Link to comment https://forums.phpfreaks.com/topic/214571-adding-values-of-array-together/#findComment-1116579 Share on other sites More sharing options...
Pikachu2000 Posted September 28, 2010 Share Posted September 28, 2010 Quite easily, akshully . . . $result2 = mysql_query("SELECT * FROM pobook WHERE jobnumber = '$jobid'"); $total = 0; while($row2 = mysql_fetch_array($result2)) { $pamount=$row2['amount']; echo "<tr>"; echo "<td>" . $pamount . "</td>"; echo "</tr>"; $total += $pamount; } echo "<tr>"; echo "<td>VALUE OF ALL RETURNED $total HERE</td>"; echo "</tr>"; Quote Link to comment https://forums.phpfreaks.com/topic/214571-adding-values-of-array-together/#findComment-1116588 Share on other sites More sharing options...
flemingmike Posted September 28, 2010 Author Share Posted September 28, 2010 im getting an error Notice: Undefined variable: ptotal in E:\Website\citycore\2\jobdetails.php on line 104 line 104 is $ptotal += $pamount; Quote Link to comment https://forums.phpfreaks.com/topic/214571-adding-values-of-array-together/#findComment-1116601 Share on other sites More sharing options...
Pikachu2000 Posted September 28, 2010 Share Posted September 28, 2010 Should be $total, not $ptotal . . . Quote Link to comment https://forums.phpfreaks.com/topic/214571-adding-values-of-array-together/#findComment-1116605 Share on other sites More sharing options...
flemingmike Posted September 28, 2010 Author Share Posted September 28, 2010 i changed it, but still same error Quote Link to comment https://forums.phpfreaks.com/topic/214571-adding-values-of-array-together/#findComment-1116609 Share on other sites More sharing options...
Pikachu2000 Posted September 28, 2010 Share Posted September 28, 2010 Post the current code block . . . Quote Link to comment https://forums.phpfreaks.com/topic/214571-adding-values-of-array-together/#findComment-1116613 Share on other sites More sharing options...
flemingmike Posted September 28, 2010 Author Share Posted September 28, 2010 here is my whole table: echo "<table border='1' style='border-collapse: collapse' bordercolorlight='#000000' bordercolordark='#000000' width='98%' align='center'>"; echo "<tr><td width='100%' colspan='9' align='center'><b>P.O.'s For Job $jobid</b></td></tr>"; echo "<tr> <th align='center'>Date</th> <th align='center'>Supplier</th> <th align='center'>Requested By</th> <th align='center'>Address of Job</th> <th align='center'>P.O. Number</th> <th align='center'>Description</th> <th align='center'>Amount</th> <th align='center'>Notes</th> <th align='center'></th> </tr>"; $result2 = mysql_query("SELECT * FROM pobook WHERE jobnumber = '$jobid' ORDER BY date"); while($row2 = mysql_fetch_array($result2)) { $pid=$row2['id']; $pdate=$row2['date']; $psupplier=$row2['supplier']; $prequestedby=$row2['requestedby']; $paddress=$row2['address']; $pjobnumber=$row2['jobnumber']; $pponumber=$row2['ponumber']; $pdescription=$row2['description']; $pamount=$row2['amount']; $pnotes=$row2['notes']; $pdate1 = date( 'M j, Y', strtotime($pdate) ); $total += $pamount; echo "<tr>"; echo "<td align='center'>" . $pdate1 . "</td>"; echo "<td align='center'>" . $psupplier . "</td>"; echo "<td align='center'>" . $prequestedby . "</td>"; echo "<td align='center'>" . $paddress . "</td>"; echo "<td align='center'>" . $pponumber . "</td>"; echo "<td align='center'>" . $pdescription . "</td>"; echo "<td align='right'>" . $pamount . "</td>"; echo "<td align='center'>" . $pnotes . "</td>"; echo "<td align='center'><a href='editpo.php?id=".$pid."'>Edit</a>"; echo "</tr>"; } echo "<tr>"; echo "<td colspan='6' align='right'><b>Total :</td>"; echo "<td align='right'><b>$" . $total . "</td>"; echo "<tdcolspan='2'></td>"; echo "</tr>"; echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/214571-adding-values-of-array-together/#findComment-1116615 Share on other sites More sharing options...
Pikachu2000 Posted September 28, 2010 Share Posted September 28, 2010 You missed a line from the first example. It was pretty obscure, but it kinda needs to be there. Should take care of the error. $result2 = mysql_query("SELECT * FROM pobook WHERE jobnumber = '$jobid' ORDER BY date"); $total = 0; // << -----ADD THIS ----------<< // while($row2 = mysql_fetch_array($result2)) Quote Link to comment https://forums.phpfreaks.com/topic/214571-adding-values-of-array-together/#findComment-1116618 Share on other sites More sharing options...
flemingmike Posted September 28, 2010 Author Share Posted September 28, 2010 thanks. didnt see that the first time Quote Link to comment https://forums.phpfreaks.com/topic/214571-adding-values-of-array-together/#findComment-1116620 Share on other sites More sharing options...
flemingmike Posted September 28, 2010 Author Share Posted September 28, 2010 thanks. didnt see that the first time Quote Link to comment https://forums.phpfreaks.com/topic/214571-adding-values-of-array-together/#findComment-1116621 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.