zang8027 Posted January 28, 2009 Share Posted January 28, 2009 I have a table with a column that pulls a decimal value from a mySQL table. Is there a way that I can sum up that entire column? <?php <table id='menuDisplayTable'> <colgroup> <col id='restCol' /> <col id='cityCol' /> <col id='priceCol' /> <col id='timeCol' /> <col id='delete' /> </colgroup> <thead> <tr> <th scope='col'>Restaurant</th> <th scope='col'>City</th> <th scope='col'>Price</th> <th scope='col'>Date</th> <th scope='col'>Delete</th> </tr> </thead> <tbody> "; //we need to color every odd row so that its easier to read. Create a row variable $rows = 1; print "<tr>"; while($row = mysql_fetch_array($result)) { $ID = $row['recID']; $rest = $row['restID']; $city = $row['cityID']; $price = $row['price']; $timedate = $row['time']; //rows print "<td>$restName</td><td>$cityName</td><td>$$price</td><td>$timedate</td><td><a href='processDeleteReceipt.php?deleteID=$ID'>Delete</a></td>"; //If the row ends, and its odd, set the tr class to odd if($rows==1) { print "<tr class='odd'>"; $rows = 0; }else{ $rows++; print "</tr>"; } //$row else end } print "<tr><td>Sum: </tr>"; print"</tbody> </table>"; mysql_close($link); } ?> Quote Link to comment Share on other sites More sharing options...
corbin Posted January 28, 2009 Share Posted January 28, 2009 SELECT SUM(column_name) FROM table; Or if you wanted to do it in PHP, psuedo code: <?php $total = 0; while($r = mysql_fetch_assoc($q)) { $total += $r['column_that_holds_value_you_want_to_add']; } I suggest doing it in MySQL though, especially if you don't plan to pull all of the values to begin with. If you do it in MySQL, it will most likely do a full table scan, so if it's a large table, you might run into issues with it, but in the same sense, if it's a large set of data, PHP would take a while too. Quote Link to comment Share on other sites More sharing options...
zang8027 Posted January 28, 2009 Author Share Posted January 28, 2009 ok, what do i set the variable to? $query="SELECT SUM(price) FROM clientReceipt WHERE userID = $userID"; $result=mysql_query($query); print "<tr>"; while($row = mysql_fetch_array($result)) { //Here is where im confused $sum = $row['price']; print "<td>Sum: $sum </td>"; } print"</tr></tbody> </table>"; Quote Link to comment Share on other sites More sharing options...
corbin Posted January 28, 2009 Share Posted January 28, 2009 You either need to use a numeric index (mysql_fetch_row or mysql_Fetch_array with numeric indexes enabled), or you'll need to use an alias: SELECT SUM(price) AS price_total FROM clientReceipt WHERE userID = $userID Then you would use price_total as the name. Quote Link to comment Share on other sites More sharing options...
zang8027 Posted January 28, 2009 Author Share Posted January 28, 2009 thank you! I learn so much here ha Quote Link to comment Share on other sites More sharing options...
corbin Posted January 28, 2009 Share Posted January 28, 2009 No problem ;p. Quote Link to comment 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.