Jump to content

[SOLVED] sum of values retrieved from database.


poleposters

Recommended Posts

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>"; }

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";}

 

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.