Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-02-2011, 04:14 PM   PM User | #1
conrad101
New to the CF scene

 
Join Date: May 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
conrad101 is an unknown quantity at this point
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..
conrad101 is offline   Reply With Quote
Old 05-02-2011, 04:35 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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);

__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 05-02-2011, 06:51 PM   PM User | #3
conrad101
New to the CF scene

 
Join Date: May 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
conrad101 is an unknown quantity at this point
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..
conrad101 is offline   Reply With Quote
Old 05-02-2011, 07:07 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.


.
mlseim is offline   Reply With Quote
Old 05-02-2011, 09:51 PM   PM User | #5
conrad101
New to the CF scene

 
Join Date: May 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
conrad101 is an unknown quantity at this point
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!
conrad101 is offline   Reply With Quote
Old 05-03-2011, 12:04 AM   PM User | #6
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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?


.
mlseim is offline   Reply With Quote
Reply

Bookmarks

Tags
basket, cookie, php, script

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:31 PM.


Advertisement
Log in to turn off these ads.