CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Problems with setting up basket Cookie (http://www.codingforums.com/showthread.php?t=225784)

conrad101 05-02-2011 04:14 PM

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!

Fou-Lu 05-02-2011 04:35 PM

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);



conrad101 05-02-2011 06:51 PM

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;
?>


mlseim 05-02-2011 07:07 PM

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.


.

conrad101 05-02-2011 09:51 PM

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!

mlseim 05-03-2011 12:04 AM

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?


.


All times are GMT +1. The time now is 11:00 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.