Page 1 of 1

New Shopping Cart Design

Posted: Fri Sep 05, 2008 5:24 pm
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");
 
?>

Re: New Shopping Cart Design

Posted: Fri Sep 05, 2008 6:32 pm
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.

Re: New Shopping Cart Design

Posted: Fri Sep 05, 2008 9:16 pm
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