Jump to content


prices sum


4 replies to this topic

#1 rannt

    Young Padawan

  • Members
  • Pip
  • 15 posts
  • Location:EU -> Slovenia -> Škofja Loka

Posted 25 September 2005 - 03:17 PM

Hi!

I'm working on a simple shopping cart, and I have problems with suming prices. Now I'm using this:

$query = "SELECT SUM(price) FROM cart WHERE sid='$sid'"
?>

This returns prices sum of all products with the same session id, BUT in table cart I also have field quantity. How can I get the quantity for every field and multiply it withe the price? And in the end of course get the sum of all prudct prices?

I hope you understand me :)

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 25 September 2005 - 03:46 PM

$sql = mysql_query("SELECT quantity, price FROM cart WHERE sid='$sid'");
while($r = mysql_fetch_assoc()){
  $total += $r['quantity']*$r['price'];
}
I assume that's fairly close to what you want done.

#3 rannt

    Young Padawan

  • Members
  • Pip
  • 15 posts
  • Location:EU -> Slovenia -> Škofja Loka

Posted 25 September 2005 - 04:11 PM

Quote

Warning: Wrong parameter count for mysql_fetch_assoc() in /www/specialci/razred/trgovina/izpis_izdelkov.php on line 37

If I use this:
$sql = mysql_query("SELECT quantity, price FROM cart WHERE sid='$sid'");
while($r = mysql_fetch_assoc($sql)){
$total += $r['quantity']*$r['price'];
}

it returns

Quote

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /www/specialci/razred/trgovina/izpis_izdelkov.php on line 37

Please help!

#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 25 September 2005 - 08:29 PM

Dang it... i knew i forgot something from that.
Anyway, try this
$sql = mysql_query("SELECT quantity, price FROM cart WHERE sid='$sid'") or die(mysql_error());
while($r = mysql_fetch_assoc($sql)){
  $total += $r['quantity']*$r['price'];
}
That error should help you figure out what's going wrong with the mysql_query()

#5 rannt

    Young Padawan

  • Members
  • Pip
  • 15 posts
  • Location:EU -> Slovenia -> Škofja Loka

Posted 26 September 2005 - 10:22 AM

OMG, I'm so stupid :tiphat: I added mysql_error and found out, that field price is missing :D
Thank you for your help!

Edited by rannt, 26 September 2005 - 10:22 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users