New Shopping Cart Design

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
<br>
Forum Commoner
Posts: 35
Joined: Thu May 01, 2008 2:42 pm

New Shopping Cart Design

Post by <br> »

My last shopping cart was a bit clumsy (viewtopic.php?f=1&t=86438).

I've redesigned it... what do you suckaz think:

Code: Select all

<?php
session_start();
 
$id=$_GET['id'];
 
//ADD
if ($_GET['action'] == 'add'){
 
    //OPTIONS (options are in the format "optionname.additionalprice")
    $style_string=$_POST['stilo'];
    if ($style_string==''){
    $style_string='stock.0';
    }
    
    //MAKE ARRAY IF DOESNT EXIST
    if(!isset($_SESSION['cart'])){
    $_SESSION['cart']=array();
    }
    
    //MAKE & POPULATE ITEM ARRAY
    $next=count($_SESSION['cart']);
    $_SESSION['cart'][$next]=array();
    $_SESSION['cart'][$next]['id']=$id;
    $_SESSION['cart'][$next]['style']=$style_string;
}
 
//DELETE
if ($_GET['action'] == 'delete'){
    unset($_SESSION['cart'][$_GET['number']]);
    sort($_SESSION['cart']);
}
 
//ADD QTY
if ($_GET['action'] == 'add_qty'){
    $_SESSION['cart'][$_GET['number']]['qty']++;
}
 
//SUBTRACT QTY
if ($_GET['action'] == 'subtract'){
    //delete or subtract one
    if ($_SESSION['cart'][$_GET['number']]['qty']==1){
        unset($_SESSION['cart'][$_GET['number']]);
        array_merge($_SESSION['cart']);
    } else {
    $_SESSION['cart'][$_GET['number']]['qty']--;
    }
}
 
 
//MERGE, COUNT
array_merge($_SESSION['cart']);
$item_count=count($_SESSION['cart']);
 
//CHECK FOR EMPTY CART
if (empty($_SESSION['cart'])){
    $nothing='nothing';
} else {
    $cart_count=0;
    while ($item_count > $cart_count){
 
        //set QTY if empty
        if (empty($_SESSION['cart'][$cart_count]['qty'])){
            $_SESSION['cart'][$cart_count]['qty']=1;
        }
        
        //fetch attributes
        $con = mysql_connect('XXX', 'XXX', 'XXX') or die(mysql_error());
        mysql_select_db('items') or die(mysql_error());
        $query_id=mysql_real_escape_string($_SESSION['cart'][$cart_count]['id'],$con);
        $query  = "SELECT * FROM items WHERE id='$query_id'";
        $result = mysql_query($query);
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
            $_SESSION['cart'][$cart_count]['baseprice']=$row['price'];
            $_SESSION['cart'][$cart_count]['name']=$row['name'];
            $_SESSION['cart'][$cart_count]['image']=$row['image'];
            $_SESSION['cart'][$cart_count]['link']='/shop/'.$row['cat'].'/'.$row['subcat'].'?id='.$row['id'];
            
            //calculate full price with options included
            $exploded_options=explode('.',$_SESSION['cart'][$cart_count]['style']);
            $name=$exploded_options[0];
            $add=$exploded_options[1];
            $_SESSION['cart'][$cart_count]['opt1']=$name;
            $_SESSION['cart'][$cart_count]['add1']=$add;
            $_SESSION['cart'][$cart_count]['fullprice']=($_SESSION['cart'][$cart_count]['baseprice']+$add);
        }
        $cart_count++;
    }
}
 
header("back to cart");
 
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: New Shopping Cart Design

Post by Christopher »

It looks like an improvement from the previous script. At least the Request processing is up front now. You could use a switch on the action perhaps.
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: New Shopping Cart Design

Post by josh »

Or put it into a object

http://www.php.net/class

and have a different class method for each action, then when you need to an add item for instance

Code: Select all

 
$cart = new cart();
$cart -> add( $itemID );
$cart -> display();
 
This way if you need to use the cart in more then one place in the site, you're not duplicating code
Post Reply