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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problems with setting up basket Cookie

    Apologies in advance as I am a noob to PHP and progaming more generally.

    Ok, I have a setcookie.php script which is set to include on other scripts, which goes as follows:

    Code:
    <?php
    
    if (isset ($_COOKIE['jumpinjakes']))  
    // do nothing if cookie 'jumpinjakes' is set
    {
    
    }
    
    else  // if cookie is unset then create cookie
    {
    $expiry = time() + 300 ; // expires variable = 5 Minutes
    $identifier = uniqid (); // function generates unique session identifier
    setcookie ("jumpinjakes[session]",$identifier,$expiry);  // sets the 'jumpinjakes' session cookie - value as per $identifier - expires in 5 min
    setcookie ("jumpinjakes[basket]"," ",$expiry); // sets the 'jumpinjakes' basket cookie - blank value - expires in 5 minutes
    
    ?>
    The Index.php script starts with:

    Code:
    <?php
    session_start(); //required for cookie to function
    ?>
    Then later a form is created, as follows:
    Code:
    <?php
     "<form action='basket.php' method='request'>".
               "<input type='hidden' value='$album' name='alb'>".
    
             "<input type='hidden' value='add' name='action'>".
               "<input type='submit' value='  add to basket  '>".
    		   
               "</form>".
    ?>
    So on clicking the button it goes to basket.php which starts with:

    Code:
    <?php
    setcookie("jumpinjakes[basket]", $_REQUEST['alb'],300);
     echo $_COOKIE['jumpinjakes']['basket'];
    
    ?>
    I am looking to write the value of 'alb' to this 'basket' cookie. I think this is the correct syntax?

    At this point I am looking to retrieve the value of [jumpinjakes][basket] cookie and assign it to a variable and echo it. So far my attempts have not worked.

    I also want to append the 'alb' value to the 'basket' cookie should the cookie already contain values so that the basket contents isn't overwritten all the time.

    My thoughts were:
    Code:
    <?php
    if ($basket) 
    {
    $basket .= ','. $alb;
    }
    
    ?>
    This is wrong, including the fact this condition will always be true.

    Any help would be very apprecicated - Thanks!
    Last edited by conrad101; 05-02-2011 at 04:21 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    session_start() is not required for cookies. Although this said you really should be using sessions instead of cookies anyway - cookies are extremely unreliable and under the full control of the user (who of course can not be trusted).
    In fact, convert this to sessions, as its a lot easier to deal with than cookies.
    PHP Code:
    <?php
    session_start
    ();
    if (!isset(
    $_SESSION['jumpinjakes']))
    {
        
    $_SESSION['jumpinjakes']['session'] = uniqid();
        
    $_SESSION['jumpinjakes']['basket'] = array();
    }
    ?>
    And in basket:
    PHP Code:
    <?php
    session_start
    ();
    if (isset(
    $_REQUEST['alb'])) // This really should be a forced GET/POST (there is no 'request' method in forms btw)
    {
        
    $alb $_REQUEST['alb'];
        if (isset(
    $_SESSION['jumpinjakes']['basket'][$alb]))
        {
            
    $_SESSION['jumpinjakes']['basket'][$alb] += 1;
        }
        else
        {
            
    $_SESSION['jumpinjakes']['basket'][$alb] = 1;
        }
    }
    I'm lazy, but something like that is what you want. You could hash the $alb itself (I hate using strings with potential spaces, I don't know why), and add in both a count and title offset to the arrays. Iteration of the above would simply be:
    PHP Code:
    foreach ($_SESSION['jumpinjakes']['basket'] AS $item => $quantity)
    {
        
    printf("%d of %s\n"$quantity$item);

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's wonderful Fou Lu. Thank you so much! It works!

    To display a basket I have done this code.

    I am trying to create a running total of the cost of the items in the basket by assigning the price value to a global float variable.

    What I've done isn't correctly totalling, could anyone suggest what I've done wrong here?

    many thanks!

    Code:
    <?php
    foreach ($_SESSION['jumpinjakes']['basket'] AS $item=> $quantity)
     {  
    printf("%d X %s\n", $quantity, $item); 
    //sql
    $query = "SELECT album, artist,retail FROM artists INNER JOIN albums ON albums.artist_id = artists.id WHERE albums.id ='$item'"; 
    //$query = "SELECT album, artist_id, retail FROM albums WHERE id = '$item'"; 
    $result = mysql_query($query);
    if (mysql_num_rows($result) > 0) {
     
    //echo results
      while($row = mysql_fetch_row($result)) {
      echo "Album Title : ".$row[0]."<br>"; // album
      echo "by ".$row[1]."<br>"; // artist
      echo "$ ".$row[2];"<br>"; // retail
    	
    	$price=(float)$row[2];// converts to float
    	$price=$price + $price;
    	GLOBAL $price;//global
       }
    
    }
    
    }
    echo "You owe $".$price;
    ?>
    Last edited by conrad101; 05-02-2011 at 07:06 PM.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Create another session variable that keeps track of the total ... as they add
    and remove items. That would be much more efficient than a query each time.

    You say "global" ... not sure what you mean by that.

    If you mean PHP "register_globals" ... that should be DISABLED (OFF).
    Usually, "off" is the server's default setting. There should be no ability
    to pass variables globally between scripts. period.


    .

  • #5
    New to the CF scene
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, I'm not sure I fully understand.

    I have included a global variable in the script because I thought that without doing that the variable $price may be out of scope after the brackets, which of course may be wrong.

    The value contained within $row[2], once converted to a float would represent the cost of the item.

    Is this statement within the while loop, wrong for producing the running total?

    Code:
    ?php
    $price=$price + $price;
    ?>
    Many thanks!

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    oh ... I thought you meant that you use the $price variable in all of your scripts.
    That you update it with one script and read it / display it with another.

    What happens when you change this:
    $price=(float)$row[2];// converts to float
    $price=$price + $price;
    GLOBAL $price;//global


    To this:
    $price=$price + $row[2];
    $price=number_format($price, 2, '.', '');


    I don't know what the float part is all about, nor the GLOBAL part.
    What is that supposed to do for you?


    .


  •  

    Tags for this Thread

    Posting Permissions

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