dynamic cookies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

fitchic77
Forum Commoner
Posts: 51
Joined: Thu Jul 20, 2006 11:57 pm

dynamic cookies

Post by fitchic77 »

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

for ($i=1; $i<=$total-1; $i++) {	
			$product =  $_POST["idProduct_".$i];
		                 setcookie("idProduct_$i", $product, time()+36001); 
		  }
I'm building dynamic cookies in order to build a shopping cart. The above are posted values from a form:

Code: Select all

<input type="text" class="inputSubmit" value="<?=$myrow2['id']?>" name="idProduct" />
I can't figure out how to loop through my cookies...

Any help would be most appreciated!!



Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by fitchic77 on Sat Oct 16, 2010 11:35 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I would seriously suggest considering a different storage than cookies for this. Why? Browsers limit both the size and number of cookies they hold for any given domain.

I would suggest using sessions instead as their size is far less restricted. As a bonus, the data isn't transmitted to or by the user on every page, only a small identification number.
fitchic77
Forum Commoner
Posts: 51
Joined: Thu Jul 20, 2006 11:57 pm

Post by fitchic77 »

Ok. but how would I find a dynamic session then?
Last edited by fitchic77 on Sat Oct 16, 2010 11:35 am, edited 1 time in total.
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post by nincha »

what do u mean by dynamic? sessions can be changed whenever you want on any page you want as long as the session is not closed (closing all browsers related to the domain). its like a super global variable. you can treat it as arrays if u want to loop.
fitchic77
Forum Commoner
Posts: 51
Joined: Thu Jul 20, 2006 11:57 pm

Post by fitchic77 »

Code: Select all

for ($i=1; $i<=10; $i++) {	
setcookie("idProduct_$i", $_POST["product_".$i], time()+36001); 
}
Please see above. This means I have a multiple cookies created ...
idProduct_1, idProduct_2 idProduct_3...idProduct_10, etc

How would I now get their values? I know how to do it in .asp, but not .php.
Last edited by fitchic77 on Sat Oct 16, 2010 11:35 am, edited 1 time in total.
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post by nincha »

treat the sessions like arrays... ex:

Code: Select all

session_start(); //intialize session
$_SESSION['items'][0] ='item 0';
$_SESSION['items'][1] ='item 1';
$_SESSION['items'][2] ='item 2';

//traverse through the list(array)
foreach($_SESSION['items'] as $eachItem){

        echo $eachItem . '<br/>';
}
fitchic77
Forum Commoner
Posts: 51
Joined: Thu Jul 20, 2006 11:57 pm

Post by fitchic77 »

Great. Thanks.

I understand what you are saying, but I'm dynamically buidling these....

$_SESSION['idProduct'][$_REQUEST['id']] = $_REQUEST['id'];


So, this:
foreach($_SESSION['idProduct'] as $eachItem){
echo $eachItem . '<br/>';
}

is always returning....one number...and the number is the item_id which is distinct. It is coming from the url here:

http://localhost/test/process.php?action=add&id=29

I need to be able to loop through all the 'idProduct' sessions in order to show my cart correctly.

:roll:
Last edited by fitchic77 on Sat Oct 16, 2010 11:36 am, edited 2 times in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Do you absolutely need to use cookies to do what you are doing?
fitchic77
Forum Commoner
Posts: 51
Joined: Thu Jul 20, 2006 11:57 pm

Post by fitchic77 »

no. I'm trying it out with Session vars.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

for ($i=1; $i<=$total-1; $i++) {       
                        $product =  $_POST["idProduct_".$i];
                                 setcookie("idProduct_$i", $product, time()+36001);
                  }
becomes..

Code: Select all

$_SESSION['products'] = array();

for ($i=1; $i<=$total-1; $i++) {       
    $_SESSION['products'][] =  $_POST["idProduct_{$i}"];
}
then on a different page..

Code: Select all

<?php 
session_start();

foreach($_SESSION['products'] as $product) {
    echo htmlentities($product);
}

?>
Remeber for sessions you must session_start(); before any output is sent to the client, and of course before any interaction with the $_SESSION superglobal can commence.

Have a read up: http://www.php.net/session
fitchic77
Forum Commoner
Posts: 51
Joined: Thu Jul 20, 2006 11:57 pm

Post by fitchic77 »

Great. You are a life saver. I would have never guessed the {} brackets...

There is catch though...the session isn't unique. In other words if I go back and add to the cart the same product twice...it ads a new item to the array that is actually a duplicate....That is why naming the session var dynamically like the cookie example is imperative for integrity:

setcookie("idProduct_$id", $id, time()+3600);
versus
$_SESSION['products'][] = $id;

I'd like to know how to build a session dynamically...like the cookie.

Also, what if you needed to also store the qty at the same time. What would be the best way to do that...store a separate cookie/session for it?
:wink:
Last edited by fitchic77 on Sat Oct 16, 2010 11:36 am, edited 1 time in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

$_SESSION['products'] = array();

for ($i=1; $i<=$total-1; $i++) {       
    $_SESSION['products']["idProduct_{$i}"] =  $_POST["idProduct_{$i}"];
}

Code: Select all

foreach ($_SESSION['products'] as $prod => $val) {
    echo htmlentities("prod $prod => $val");
}
:)
fitchic77
Forum Commoner
Posts: 51
Joined: Thu Jul 20, 2006 11:57 pm

Post by fitchic77 »

WOW. The things you learn from others....THANKS a Million....AGAIN!

Just really quick....

I need to store the qty at the same time. What would be the best way to do that...store a separate cookie/session for it? That is what I'm thinking.
Last edited by fitchic77 on Sat Oct 16, 2010 11:36 am, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

No way is Jenk the best. It has to be me. :wink:

Since you are using sessions for this script, you can use a session var to store quantities also. If you need to store an array of them, use a similar routine as what Jenk posted. If it is a single value, set it like this...

Code: Select all

<?php
$_SESSION['quantity'] = $thequantityyouwanttostore;
?>
fitchic77
Forum Commoner
Posts: 51
Joined: Thu Jul 20, 2006 11:57 pm

Post by fitchic77 »

Ok. Well the code all dynamic based on the ID of the product....

Code: Select all

$_SESSION['products']["idProduct_{$id}"]  =  $id;
I'm going to assume for the qty I'll need:

Code: Select all

$_SESSION['qty']["idqty_{$id}"]  =  $qty;
Last edited by fitchic77 on Sat Oct 16, 2010 11:35 am, edited 1 time in total.
Post Reply