SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cookie refresh problem...

    Hi,

    Ok I am currently making a shopping cart and have a problem. I am using cookies to keep track of a users shopping cart and when they add an item, I need it to redirect them to the product the just ordered but also, to show a little summary of their cart on the page. Now this is the code I am using:

    PHP Code:
    $product $_REQUEST['pid'];
    $quantity 1;
    $cart = new Cart($cart_id); 
    $cart->add_item($product$quantity);
    echo 
    "<h2>Item Added to Shopping Basket</h2>";
    echo 
    "<p>Thank you for adding the item to your shopping basket.</p>";
    echo 
    "<p><a href=\"index.php?section=Checkout\">Checkout</a> or ";
    echo 
    "<a href=\"".$_SERVER['HTTP_REFERER']."\">go back to previous product.</a></p>"
    Now, the above code kicks in when the user clicks the Add to Basket button.

    This is some of the class I am using that you saw initiated above:

    PHP Code:
    // Add item to cart
    function add_item($product$quantity) {
        
        
    // Select data for product
        
    $query "SELECT fld_quantity FROM " $this->inv_table .
        
    " WHERE fld_pid='$product'";
        
        
    $result mysql_query($query$this->dblink);
        
        
    $row mysql_fetch_array($result);
        
        
    // Quantity greater than stock level
        
    if($quantity $row['fld_quantity']) return false;
        
        
    mysql_free_result($result);
        
        
    // Check not ordering more than total amount
        
    $qty $this->check_item($product);
        
        
    // Product not already added to cart, add new
        
    if($qty == 0) {
            
            
    // Insert into database
            
    $query "INSERT INTO ".$this->cart_table
            
    " (fld_id, fld_pid, fld_cartid, fld_amount, fld_date, fld_time) VALUES ";
            
    $query .= "('', '$product', '".$this->cart_id."',  '$quantity',CURRENT_DATE(), CURRENT_TIME()) ";
            
    mysql_query($query$this->dblink);
        
        } else { 
    // Product already added to cart, update quantity for that product
            
            // Add one to quantity
            
    $quantity += $qty;
            
            
    // Update database
            
    $query "UPDATE ".$this->cart_table .
            
    " SET fld_amount='$quantity' WHERE fld_cartid='".$this->cart_id."' AND ";
            
    $query .= "fld_pid='$product' ";
            
    mysql_query($query$this->dblink);
            
        }
        return 
    true;

    This is the cookie being set for $cart_id:

    PHP Code:
    // no cookie set for cart
    if(!isset($HTTP_COOKIE_VARS['DCP_Cart'])) {
        
        
    // create random cart id
        
    $cart_id md5(uniqid(rand()));
        
        
    // set cookie for cart
        
    setcookie("DCP_Cart"$cart_idtime() + 14400);

    // cart cookie already set, use it
    } else {
        
        
    $cart_id $HTTP_COOKIE_VARS['DCP_Cart'];


    This is the script that shows if something has been added to the cart:

    PHP Code:
    <?
    // check to see if there are items in the users cart
    $cart = new Cart($cart_id);
    if (
    $cart->num_items() > 0) { ?>

    <img src="../_img/img_cart.jpg" alt=" " width="17" height="34" title=" "/><br />
    </br><a href="#"> <? echo _txt_VALIDATEORDER_?></a> 
        
    <? ?>
    Any ideas on how to get this to show as soon as I add a product...the problem seems to be where I say thankyou, I would like to have a refresh of the page there but I can't do it with the PHP Location function as it's not at the top of the page and it gives me errors about headers already being sent.

    Anyone think of a better way to update this?

    Cheers,
    Chris

  2. #2
    SitePoint Member sandeepshinde's Avatar
    Join Date
    Nov 2004
    Location
    India
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you set the cookie the cookie is not reflected as soon as you set it.

    It would be a better idea that you set the cookie as soon as anybody hits your site
    i.e you can set the cookie on the index page itself so you can get the cookie in the rest of the site


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
  •