Calculating shopping cart

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Calculating shopping cart

Post by Zeceer »

Hi,

I'm trying to calculate the total price in my shopping cart. When looping thru, the script must get the price from the database since only the id and quantity is stored in the session array. The first part that involves counting quantities works fine, but to get the price from the database, and multiply this by the products quantity, does not work som fine. I get an error on this line:

Code: Select all

$result = mysql_query( "SELECT * FROM products WHERE prodnr='".$key."'" );
Heres the loop in the script:

Code: Select all

foreach( $HTTP_SESSION_VARSї'items'] as $key=>$value )
    {
        //The quantity of products

        $quantity = $number+$value;
        $number = $quantity;
        
        //The price of the products
        
        $link = mysql_connect( $hostname, $username, $password );

        mysql_select_db( $db )
                 or die ( "Couldn't open $db: ".mysql_error() );
                 
        $cart_total = 0

        $result = mysql_query( "SELECT * FROM products WHERE prodnr='".$key."'" );
        $row = mysql_fetch_assoc($result);
        
        $cart_total = $cart_total + $rowї"price"] * $value;
    }
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

What error do you get?
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

Here you go mate :D
Parse error: parse error, unexpected T_VARIABLE in E:\Web Server\inc shop v2.0\cart_summary.php on line 31
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

A missing semicolon on the line before...

$cart_total = 0 ;
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

yea, hehe DOH! I hate those ones...
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

There! Ahhh, it works :D. I have a tendence to overlook those small errors in the coding after coding a while :oops:
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

Kven kveit sa mannen :p
Post Reply