Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Banned
    Join Date
    Oct 2007
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Remove Product Quantity's

    I'm trying to develop a simple shopping basket, how do I remove the quantity option from the following code, so that only one of each item can be purchased instead of 2 or more?

    Also, if the product selected to add to basket, already exists in the shopping cart, how do I display a message notifying the user?

    Code:
    <?php
    
    session_start();
    $con = mysql_connect("localhost","ODBC","");
    if (!$con)
    {
      die(mysql_error());
    }
    
    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>';
    }
    }
    
    $cart = $_SESSION['cart'];
    if ($cart) {
    $cart .= ','.$_GET['id'];
    } else {
    $cart = $_GET['id'];
    }
    $_SESSION['cart'] = $cart;
    
    
    $cart = $_SESSION['cart'];
    if ($cart) {
    $items = explode(',',$cart);
    $contents = array();
    foreach ($items as $item) {
    $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
    }
    }
    
    $total = 0;
    echo '<table>';
    
    foreach ($contents as $id=>$qty) {
    mysql_select_db("MP3", $con);
    $result = mysql_query("SELECT * FROM books WHERE id = '$id'") or die("Your have an error because:<br />" . mysql_error());
    $row = mysql_fetch_assoc($result);
    
    
    echo '<tr>';
    echo '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
    echo '<td>'.$row['title'].' by '.$row['author'].'</td>';
    echo '<td>&pound;'.$price.'</td>';
    echo '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
    echo '<td>&pound;'.($price * $qty).'</td>';
    $total += $price * $qty;
    echo '</tr>';
    }
    echo '</table>';
    echo '<p>Grand total: &pound;'.$total.'</p>';
    
    ?>

  • #2
    Regular Coder Rappa's Avatar
    Join Date
    Nov 2005
    Location
    LI
    Posts
    356
    Thanks
    21
    Thanked 9 Times in 9 Posts
    well I don't do php but I assume its as simple as:

    if ($qty != 1)
    error
    else
    calculate total


    Apologies if i'm wrong.


  •  

    Posting Permissions

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