Display shopping cart using PHP & HTML

Discussion in 'PHP' started by siyah miyah, Apr 28, 2008.

  1. #1
    I have finally managed to create a shopping cart that works using:

    <?php

    session_start ();

    include ('connection.php');

    // 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
    if (isset($_SESSION['cart'] [$id] ))
    $_SESSION['cart'] [$id]++;
    else
    $_SESSION['cart'] [$id] = 1;
    }

    //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];
    }
    }
    ?>

    AND

    <?php
    //displays products
    if (isset($_SESSION['cart']) && is_array ($_SESSION['cart'])){
    echo '<div id="product">';
    echo '<h3>My Basket</h3>';
    echo '<table width="600" border="0" cellpadding=0 cellspacing=5>';
    echo '<form name="userForm" method="post" action="sbasket.php">';
    echo '<tr><td width="200" scope="col" align="center">Product:</td>';
    echo '<td width="100" align="right">Quantity: </td>';
    echo '<td width="100" align="right">Price:</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_array($result);

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

    echo '<tr><td align = "left"><span>';
    echo $product['p_name'];
    echo '</span></td>';
    echo '<td align = "right"><span>';
    //name of textbox=prodid and value=qty
    echo '<input name="'.$id.'" type="text" size="3" maxlength="3" value="'.$qty.'">';
    echo '</span></td>';
    echo '<td align = "right"><span>&pound;';
    echo $tprice;
    echo '</span></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 weeks: '.$_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>';
    ?>

    Now in the area that I have "display products" area, which is bold, I want the attributes to come up such as product name, price, etc...I tried setting a variable, such as:
    $name= $_GET['name'];

    and using the '.$name.' do display the details of the product but it does not work...nor is the display of the table very nice...Can anyone help?
     
    siyah miyah, Apr 28, 2008 IP
  2. ggggqqqqihc

    ggggqqqqihc Peon

    Messages:
    92
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    change this:
    to:
    $product = mysql_fetch_assoc($result);

    mysql_fetch_array() returns an integer-indexed array, i.e. $product[0], $product[1]... but mysql_fetch_assoc() returns an associative array.
     
    ggggqqqqihc, Apr 28, 2008 IP