SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can anyone help with a shopping cart script?

    Hi All,

    I'm trying to modify a shopping cart script to suit my needs. Now it's working well, but I just have one problem:

    http://www.inspireaway.co.uk/cart/

    Now as you can see from this page, if you click on Add to Cart it adds one of those products, but what i'd ideally like to do is add as many of those products picked from the dropdown list next to each product.

    However, I have no idea how and if this can be done.
    Last edited by chris_stfc; May 30, 2009 at 13:30.

  2. #2
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to put the select tag into a form:
    Code:
    <form action="cart.php" method="post">
    <select name="quantity" id="quantity">
     <option selected="selected" value="1">1</option>
     <option value="2">2</option>
     <option value="3">3</option>
     <option value="4">4</option>
     <option value="5">5</option>
     </select>
    <input type="submit" name="submit" />
    </form>
    and in cart.php test the post array
    PHP Code:
    <?php
    ini_set
    ("display_errors"1);
    error_reporting(E_ALL);
    print_r($_POST);
    //...
    my mobile portal
    ghiris.ro

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much for helping me.

    I've updated the Cart accoringly:

    http://www.inspireaway.co.uk/cart/

    But the Cart still ONLY updates one of those values nomatter how many you select from the dropdown list:

    index.php

    PHP Code:
    <?php
    // Start the session
    session_start();
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>PHP Shopping Cart Demo · Bookshop</title>
        <link rel="stylesheet" href="css/styles.css" />
    </head>

    <body>

    <div id="shoppingcart">

    <h1>Your Shopping Cart</h1>

    <?php
    function writeShoppingCart() {
        
    $cart $_SESSION['cart'];
        if (!
    $cart) {
            return 
    '<p>You have no items in your shopping cart</p>';
        } else {
            
    // Parse the cart session variable
            
    $items explode(',',$cart);
            
    $s = (count($items) > 1) ? 's':'';
            return 
    '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
        }
    }

    echo 
    writeShoppingCart();
    ?>

    </div>

    <div id="booklist">

    <h1>Books In Our Store</h1>

    <?php
    $sql 
    'SELECT * FROM books ORDER BY id';
    $result $db->query($sql);
    $output[] = '<ul>';
    while (
    $row $result->fetch()) {
        
    $output[] = '<li>"'.$row['title'].'" by '.$row['author'].': &pound;'.$row['price'].'<br /></li>
                        <form action="cart.php?action=add&id=&id='
    .$row['id'].'" method="post">
    <select name="qty" id="qty">
     <option selected="selected" value="1">1</option>
     <option value="2">2</option>
     <option value="3">3</option>
     <option value="4">4</option>
     <option value="5">5</option>
     </select>
    <input type="submit" value="Add to Cart" name="submit" />
    </form>

    '
    ;
    }
    $output[] = '</ul>';
    echo 
    join('',$output);
    ?>

    </div>

    </body>
    </html>
    cart.php

    PHP Code:
    <?php
    // Start the session
    session_start();
    // Process actions

    ini_set("display_errors"1); error_reporting(E_ALL); print_r($_POST); 


    $cart $_SESSION['cart'];
    $action $_GET['action'];
    switch (
    $action) {
        case 
    'add':
            if (
    $cart) {
                
    $cart .= ','.$_GET['id'];
            } else {
                
    $cart $_GET['id'];
            }
            break;
        case 
    'delete':
            if (
    $cart) {
                
    $items explode(',',$cart);
                
    $newcart '';
                foreach (
    $items as $item) {
                    if (
    $_GET['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')) {
                    
    $id str_replace('qty','',$key);
                    
    $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
                    
    $newcart '';
                    foreach (
    $items as $item) {
                        if (
    $id != $item) {
                            if (
    $newcart != '') {
                                
    $newcart .= ','.$item;
                            } else {
                                
    $newcart $item;
                            }
                        }
                    }
                    for (
    $i=1;$i<=$value;$i++) {
                        if (
    $newcart != '') {
                            
    $newcart .= ','.$id;
                        } else {
                            
    $newcart $id;
                        }
                    }
                }
            }
        }
        
    $cart $newcart;
        break;
    }
    $_SESSION['cart'] = $cart;
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>PHP Shopping Cart Demo · Cart</title>
        <link rel="stylesheet" href="css/styles.css" />
    </head>

    <body>

    <div id="shoppingcart">

    <h1>Your Shopping Cart</h1>

    <?php

    function writeShoppingCart() {
        
    $cart $_SESSION['cart'];
        if (!
    $cart) {
            return 
    '<p>You have no items in your shopping cart</p>';
        } else {
            
    // Parse the cart session variable
            
    $items explode(',',$cart);
            
    $s = (count($items) > 1) ? 's':'';
            return 
    '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
        }
    }

    echo 
    writeShoppingCart();
    ?>

    </div>

    <div id="contents">

    <h1>Please check quantities...</h1>

    <?php

    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>';
            foreach (
    $contents as $id=>$qty) {
                
    $sql 'SELECT * FROM books WHERE id = '.$id;
                
    $result $db->query($sql);
                
    $row $result->fetch();
                
    extract($row);
                
    $output[] = '<tr>';
                
    $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
                
    $output[] = '<td>'.$title.' by '.$author.'</td>';
                
    $output[] = '<td>&pound;'.$price.'</td>';
                
    $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
                
    $output[] = '<td>&pound;'.($price $qty).'</td>';
                
    $total += $price $qty;
                
    $output[] = '</tr>';
            }
            
    $output[] = '</table>';
            
    $output[] = '<p>Grand total: <strong>&pound;'.$total.'</strong></p>';
            
    $output[] = '<div><button type="submit">Update cart</button></div>';
            
    $output[] = '</form>';
        } else {
            
    $output[] = '<p>You shopping cart is empty.</p>';
        }
        return 
    join('',$output);
    }


    echo 
    showCart();
    ?>

    <p><a href="index.php">Back to bookshop...</a></p>

    </div>

    </body>
    </html>
    Any other ideas?

    Thanks


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
  •