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
    Nov 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Couple of Questions

    The following code is what I have so far to create a simple shopping cart. Is there a different way of adding the id number of the product into the array? - As I don't 100% understand what is happening. How do I notify the user that "The Item already exists", if the cart already contains their selected item?

    Code:
    $cart = $_SESSION['cart'];
    if ($cart) {
    $cart .= ','.$_GET['id'];
    } else {
    $cart = $_GET['id'];
    }
    $_SESSION['cart'] = $cart;
    How do I delete records from the shopping cart?

    Code:
    <?php
    session_start();
    
    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;
    
    
    
    
    $total = 0;
    echo '<table>';
    $id = $_GET['id'];
    
    mysql_select_db("MP3", $con);
    
    $items = explode(',',$cart);
    echo $cart;
    foreach ($items as $item)
    
    {
    $result = mysql_query("SELECT * FROM books WHERE id = '$item'") or die("Your have an error because:<br />" . mysql_error());
    while($row = mysql_fetch_array($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;'.$row['price'].'</td>';
    $total = $total + $row['price'];
    echo '</tr>';
    }
    }
    echo '</table>';
    echo '<p>Grand total: &pound;'.$total.'</p>';
    ?>
    Also, I have a how do I put the add and delete code into a switch statement? I have the following code.

    Code:
    switch ($action)
    {
      case 'add':
                   break;
    
      case 'delete':
                       break;
    }

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I always recommend you understand what your code is doing.

    It looks like you're using a single string variable $cart to store all the different product IDs in your shopping cart (delimited by a comma). Rather than doing that, I suggest you use an array instead. Simply add an element to the array when the user adds a product to the shopping cart. This will make it simple to delete items as well. You can even store product quantity in this array. And calculating the total amount due becomes a simple matter as well.

    In case you are not familiar with how to work with arrays, study the manual-- it has a lot of good information about arrays.

    http://us2.php.net/manual/en/language.types.array.php


  •  

    Posting Permissions

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