SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with adding sizes to cart page

    hello everyone,
    I've been racking my brain with this...
    I'm trying to display the sizes of shirts when added to my cart.
    The sizes come from a select menu.
    The sizes show up in the cart, but overwrite the last size added.

    For example ..
    add shirt1 size-small
    add shirt2 size-medium

    the cart displays...
    shirt 1 size-medium
    shirt 2 size-medium

    Can you please help me so when I add a new item it doesn't overwrite the previous size.

    all help is appreciated

    my code is as follows....

    selection from my product page
    Code:
    <select name="shirt_size">
    <option value="x-small">x-small</option>
    <option value="small">small</option>
    <option value="medium">medium</option>
    <option value="large">large</option>
    </select>
    code that displays shirt size
    PHP Code:
    $output[] = '<td>'.$_POST['shirt_size'].'</td>'
    function displaying my cart
    PHP Code:
    function showCart() {
        global 
    $db;
        
    $cart $_SESSION['cart'];
        if (
    $cart) {
            
    $items explode(',',$cart);
            
    $contents = array();
            foreach (
    $items as $item) {
                
    $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1;
            }
            
    $output[] = '<form action="cart.php?action=update" method="post" id="cart">';
            
    $output[] = '<table>';
                
    // start div
                
    $output[] = '<div id="cart_table">';
                
    //
                
    $output[] = '<tr>';
                
    $output[] = '<td><h4>Product</h4></td>';
                
    $output[] = '<td><h4>Item No.</h4></td>';
                
    $output[] = '<td><h4>Price</h4></td>';
                
    $output[] = '<td><h4>Size</h4></td>';
                
    $output[] = '<td><h4>Quantity</h4></td>';
                
    $output[] = '<td><h4>Price Total</h4></td>';
                
    $output[] = '<td><h4>&nbsp;</h4></td>';
                
    $output[] = '</tr>';
                
    //new row
            
    foreach ($contents as $product_id=>$qty) {
                
    $sql 'SELECT * FROM products WHERE product_id = '.$product_id;
                
    $result $db->query($sql);
                
    $row $result->fetch();
                
    extract($row);
                
    //
                
    $output[] = '<tr>';
                
    $output[] = '<td><a href="product.php?product_id='.$product_id.'">'.$product_title.'</a></td>';
                
    $output[] = '<td>'.$product_plu.'</td>';
                
    $output[] = '<td>$'.$product_price.'</td>';
                
    //
                
    $output[] = '<td>'.$_POST['shirt_size'].'</td>';
                
    //
                
    $output[] = '<td><input type="text" name="qty'.$product_id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
                
    $output[] = '<td>X&nbsp;$'.($product_price $qty).'</td>';
                
    $total += $product_price $qty;
                
    $output[] = '<td><a href="cart.php?action=delete&product_id='.$product_id.'" class="r">Remove</a>&nbsp;</td>';
                
    $output[] = '</tr>';
                
    //end div
                
    $output[] = '</div>';
                
                
                
            }
            
    $output[] = '</table>';
            
    $output[] = '<p>Grand total: <strong>$'.$total.'</strong></p>';
            
    $output[] = '<div class="float-right"><button type="submit">Update cart</button>';
            
    $output[] = '</form>'

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Show us the code for adding something to the cart.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry bout that

    PHP Code:
    session_start();
    // Process actions
    $cart $_SESSION['cart'];
    $action $_GET['action'];
    switch (
    $action) {
        case 
    'empty':
            if(
    $cart) {
                unset(
    $cart);
            }
            break;
        case 
    'add':
            if (
    $cart) {
                
    $cart .= ','.$_GET['product_id'];
            } else {
                
    $cart $_GET['product_id'];
            }
            break;
        case 
    'delete':
            if (
    $cart) {
                
    $items explode(',',$cart);
                
    $newcart '';
                foreach (
    $items as $item) {
                    if (
    $_GET['product_id'] != $item) {
                        if (
    $newcart != '') {
                            
    $newcart .= ','.$item;
                        } else {
                            
    $newcart $item;
                        }
                    }
                }
                
    $cart $newcart;
            }
            break;
        case 
    'update':
        if (
    $cart) {
            
    $newcart '';
            foreach (
    $_POST as $key=>$value) {
                if (
    stristr($key,'qty')) {
                    
    $product_id str_replace('qty','',$key);
                    
    $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
                    
    $newcart '';
                    foreach (
    $items as $item) {
                        if (
    $product_id != $item) {
                            if (
    $newcart != '') {
                                
    $newcart .= ','.$item;
                            } else {
                                
    $newcart $item;
                            }
                        }
                    }
                    for (
    $i=1;$i<=$value;$i++) {
                        if (
    $newcart != '') {
                            
    $newcart .= ','.$product_id;
                        } else {
                            
    $newcart $product_id;
                        }
                    }
                }
            }
        }
        
    $cart $newcart;
        break;
    }
    $_SESSION['cart'] = $cart

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Okay... you're not storing size information in the cart. Only thing you're storing is the product ID. So the system doesnt know what size the person ordered.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried this... but I still can't get it right.
    Any idea what I'm doing wrong?

    PHP Code:
     case 'add':
            if (
    $cart) {
                
    $cart .= ','.$_GET['product_id']['shirt_size'];
            } else {
                
    $cart $_GET['product_id']['shirt_size'];
            }
            break; 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •