SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict Eric1776's Avatar
    Join Date
    May 2002
    Location
    Atlanta, GA
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Shopping Cart Problems

    I'm trying to build a simple shopping cart as more of a learning experience than anything else. I have Kevin's book and its been a great help, but I still have a problem.

    This is the code in the header of my viewcart.php file"
    PHP Code:
    ////////////////////// Begin Shopping Cart Session //////////////////
    $shoppingcart = array();
    session_start();
    session_register('shoppingcart');
    $totalitems =count($shoppingcart);
    if (
    $buy != "") {
        
    // Add item to the end of the $shoppingcart array
        
    $shoppingcart[] = $buy;
        
    header("Location: $PHP_SELF?".SID);
        exit();
    }
    if (
    $empty != "") {
        
    // Empty the $shoppingcart array
        
    session_unregister("shoppingcart");
        
    header("Location: $PHP_SELF?".SID);
        exit();
    }
    ////////////////////// End Shopping Cart Session //////////////////// 
    When someone is browsing products and clicks on the "Add to Cart" link it comes to this page and adds the item to the shoppingcart array which is set as a session.

    I placed this code in my page:
    PHP Code:
        for ( $i =0$i count($shoppingcart); $i++ )
        echo 
    "$shoppingcart[$i] <br>"
    and it displays a list of all of the items the visitor has added during that session.

    The problem is when they add the product it sends the $buy variable that is equal to the productID. So all that shows on my cart is a list of product IDs. How do I associate those productIds with the productIds contained in my product table and display the product details?
    I hope this makes sense.

    For example right now I recieve the following results:
    You have added the following products to your cart:
    4
    2
    5

    I want it to show this:
    You have added the following products to your cart:
    Product4 | This is the description | $30.77
    Product2 | This is the description | $14.99
    Product5 | This is the description | $10.78
    Last edited by Eric1776; Sep 18, 2002 at 10:11.

  2. #2
    SitePoint Member HanKK's Avatar
    Join Date
    Sep 2004
    Location
    New Zealand
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Given that a table called Product exists with attributes description and price, just use ID of the product that has been added to the cart to extract its own attribute information from table Product using sql query.

    Something like this perhaps.
    PHP Code:
    for ( $i =0$i count($shoppingcart); $i++ ) 
    {
      echo 
    "Product" $shoppingcart[$i] . " | " ;
         
      
    $sql ="select description,price
                 from Product 
                 where pid ='
    $shoppingcart[$i]'";

      
    $result mysql_query($sql);
       
      
    $row mysql_fetch_array($result);

      echo 
    $row['description'] . " | " $row['price'] . "<br>";

    Last edited by HanKK; Oct 9, 2004 at 04:22.


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
  •