poleposters Posted October 23, 2008 Share Posted October 23, 2008 I have some prices stored in a database. I retrieve them using a while loop to print them out. But now I need a way to add the numbers retrieved together. I"ve tried phrasing it a few different ways in google but can't find anything that helps. Can anyone help? Here the code if it helps. Although I'd be happy with just a strategy to attack this. $query="SELECT * FROM items WHERE order_id='$action'"; $result=mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error()); $numlinks=mysql_num_rows($result); if ($numlinks>0) { while($selectlinks=mysql_fetch_array($result)) { $itemdetails=$selectlinks['item']; $itemid=$selectlinks['item_id']; $price2=$selectlinks['price']; $price="$".$price2; print "<div class=\"item\">"; print "<div class=\"itemheading\"><div class=\"product\"><h2>$itemdetails</h2></div><div class=\"price\"><h2>$price</h2></div><div class=\"remove\"> <a href=\"removeitem.php?itemnumber=$itemid&order=$action\">Delete item</a></div></div>"; $query2="SELECT * FROM artwork WHERE item_id='$itemid'"; $result2=mysql_query($query2) or trigger_error("Query: $query2\n<br />MySQL Error: " . mysql_error()); $numlinks2=mysql_num_rows($result2); if($numlinks2>0) { while($selectlinks2=mysql_fetch_array($result2)) { $artwork2=$selectlinks2['artwork']; $artwork = $ext = substr($artwork2, strrpos($artwork2, '/') + 1); $artworkid=$selectlinks2['artwork_id']; print "<div class=\"art\"><a href=\"$artwork2\">$artwork</a></div><div class=\"remove\"><a href=\"removeartwork.php?artworkid=$artworkid&orderid=$action\">Remove</a></div>"; } } $itemdetails2=str_replace(" ", "_", $itemdetails); $artname=$itemdetails2."_".$name; print "<div class=\"itemheading\"><a href=\"uploadartwork.php?item=$artname&itemnumber=$itemid&order=$action\">Attach artwork</a></div>";print "</div>"; } Link to comment https://forums.phpfreaks.com/topic/129762-solved-sum-of-values-retrieved-from-database/ Share on other sites More sharing options...
dropfaith Posted October 23, 2008 Share Posted October 23, 2008 http://www.tizag.com/mysqlTutorial/mysqlsum.php Link to comment https://forums.phpfreaks.com/topic/129762-solved-sum-of-values-retrieved-from-database/#findComment-672719 Share on other sites More sharing options...
GKWelding Posted October 23, 2008 Share Posted October 23, 2008 if the prices are stored in an array use the array_sum() function... Link to comment https://forums.phpfreaks.com/topic/129762-solved-sum-of-values-retrieved-from-database/#findComment-672721 Share on other sites More sharing options...
poleposters Posted October 23, 2008 Author Share Posted October 23, 2008 Thanks for the link to the sum function. That looks like what I need. But I'm not getting a result.(or an error) Is my syntax right? $query4="SELECT sum(price) FROM items WHERE order_id='$action'"; $result4=mysql_query($query4) or trigger_error("Query: $query4\n<br />MySQL Error: " . mysql_error()); while($row = mysql_fetch_array($result4)){ $sub=$row['SUM(price)']; print "$sub";} Link to comment https://forums.phpfreaks.com/topic/129762-solved-sum-of-values-retrieved-from-database/#findComment-672740 Share on other sites More sharing options...
poleposters Posted October 23, 2008 Author Share Posted October 23, 2008 I worked it out. I didn't realise that the sum function was case sensitive. I just changed it to uppercase and it works. Thanks for the help! Link to comment https://forums.phpfreaks.com/topic/129762-solved-sum-of-values-retrieved-from-database/#findComment-672744 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.