Page 1 of 1

Simple but silly :-) - Please Help :-)

Posted: Thu Jun 04, 2009 5:24 pm
by netties001
Hi, i'm new to this site and pretty new to PHP and I have a really simple but silly question for everyone.

Basically, at the moment I'm trying to return the total sum of a price column from a database I have. I've built my own shopping cart as a side project and would like to know how to add the total values of the cart for that user together.

Here's what I have so far...

Code: Select all

$query2="SELECT * FROM orders_temp WHERE order_username = '" . $_SESSION["user_name"] . "'";
$result2=mysql_query($query2);
 
while($r1=mysql_fetch_array($result2))
    {
        $order_product_price=$r1["order_product_price"];
 
}
How do I add the result of all the prices together to get a total cost for the user?

Sorry, probably seems a simple thing but I've looked all over google and it might as well be in spanish to me lol :-)

Thanks in Advance.
Netties :drunk:

Re: Simple but silly :-) - Please Help :-)

Posted: Thu Jun 04, 2009 5:28 pm
by Darhazer

Code: Select all

$total = 0;
while($r1=mysql_fetch_array($result2))
{
$total += $r1["order_product_price"];
}

Re: Simple but silly :-) - Please Help :-)

Posted: Thu Jun 04, 2009 5:33 pm
by netties001
You're a star cheers! :-)

The value isn't showing anything after the decimel point, i.e. £10.00 is showing as £10 ? - any advice?

Thanks,
Netties.

Re: Simple but silly :-) - Please Help :-)

Posted: Thu Jun 04, 2009 5:53 pm
by mikemike
See number_format() - that'll sort it for you.

Re: Simple but silly :-) - Please Help :-)

Posted: Thu Jun 04, 2009 8:14 pm
by netties001
Thanks for the help, it totally solved it - cheers ;-) much appreciated.