Adding a delete/empty to a shopping cart

Discussion in 'PHP' started by siyah miyah, May 1, 2008.

  1. #1
    Using the following code, how would I add a delete and empty cart array to the shopping basket...I tried to define the session which is in bold.
    <?php
    include ('connection.php');

    session_start ();

    // define session variable
    $_SESSION['cart'];

    //if product exists in command line, add it to cart
    @$id = $_GET['prod_id'];



    //checks if command line has product added
    if($id){
    //checks if cart is empty & creates it
    if(!isset($_SESSION['cart'])){
    $_SESSION['cart'] = array();
    $_SESSION['items'] = 0;
    $_SESSION['total_price'] = '0.00';
    }

    //cart array contains productid & quantity - add
    if (isset($_SESSION['cart'] [$id] ))
    $_SESSION['cart'] [$id]++;
    else
    $_SESSION['cart'] [$id] = 1;
    }
    //remove
    if (isset($_SESSION['cart'] [$id] ))
    $_SESSION['cart'] [$id]--;
    else
    $_SESSION['cart'] [$id] = 0;
    }


    //checks if command line has product updated
    if(isset($_POST['update'])){
    foreach ($_SESSION['cart'] as $id => $qty){
    //value from text box name
    if($_POST[$id]=='0')
    unset($_SESSION['cart'] [$id]);
    else
    $_SESSION['cart'] [$id] = $_POST[$id];
    }
    }
    ?>

    <?php
    //displays products
    if (isset($_SESSION['cart']) && is_array ($_SESSION['cart'])){
    echo '<div id="product">';
    echo '<h2>My Basket</h2>';
    echo '<table width="500" border="0" cellpadding=0 cellspacing=5>';
    echo '<form name="userForm" method="post" action="sbasket.php">';
    echo '<tr><td width="200" scope="col" align="center"><h2>Product: '.$id['p_name'].' </h2></td>';
    echo '<td width="150" align="right"><h2>Rental Wks: '.$id['qty'].' </h2></td>';
    echo '<td width="150" align="right"><h2>Price/Wk: '.$id['price_wk'].'</h2></td></tr>';
    $_SESSION['total_price'] = '0.00';
    $_SESSION['items'] = 0;

    //place lines of products in page
    foreach ($_SESSION['cart'] as $id => $qty){
    $query = 'select * from products where prod_id = '.$id.' ';
    $result = mysql_query($query);
    if (!$result){
    mysql_close($conn);
    return -2;
    }
    $catNo = mysql_num_rows($result);
    if ($catNo == 0){
    mysql_close($conn);
    return 0;
    }
    $product = array();
    $product = mysql_fetch_assoc($result);

    $tprice = $qty*$product['price_wk'];

    echo '<tr><td align = "left"><h2><span>';
    echo $product['p_name'];
    echo '</span></h2></td>';
    echo '<td align = "right"><h2><span>';
    //name of textbox=prodid and value=qty
    echo '<input name="'.$id.'" type="text" size="3" maxlength="3" value="'.$qty.'">';
    echo '</span></h2></td>';
    echo '<td align = "right"><h2><span>&pound;';
    echo $tprice;
    echo '</span></h2></td></tr>';

    $_SESSION['items'] += $qty;
    $_SESSION['total_price'] += $tprice;
    }

    //starts the update qty proceedure
    echo '<input type="hidden" name="update" value=true>';
    echo '<tr bgcolor="#999999"><td align="center"><input type="submit" name="Submit" value="Update Shopping Basket"></td>
    <td align = "right"><b>Number of Wks: '.$_SESSION['items'].'</b></td><td align="right"><b>Total Price: &pound;'.$_SESSION['total_price'].'</b></td></tr>';
    echo '</form>';
    echo '</table>';
    echo '</div>';

    }else{
    //if no product in cart
    echo '<div id="information">';
    echo '<h3><span>No products in Shopping basket</span></h3>';
    echo '<p class="p1"><span>Please login and add products to your shopping basket</span></p>';
    echo '</div>';
    }


    //further options
    echo '<div id="product">';
    echo '<h3>Further options</h3>';
    if(isset($_SESSION['loggedUser'])){
    echo '<form name="furForm" method="post" action="finalizeorder.php"><input type="submit" name="Submit" value="Finalize Order"></form>';
    }
    else{
    echo '<form name="furForm" method="post" action="register.php"><input type="submit" name="Submit" value="Please login to finalize your order"></form>';
    }
    echo '</div>';
    ?>
     
    siyah miyah, May 1, 2008 IP