Page 1 of 2
dynamic cookies
Posted: Tue Aug 08, 2006 1:59 pm
by fitchic77
Everah | Please use 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
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]
Posted: Tue Aug 08, 2006 2:03 pm
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.
Posted: Tue Aug 08, 2006 2:05 pm
by fitchic77
Ok. but how would I find a dynamic session then?
Posted: Tue Aug 08, 2006 2:38 pm
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.
Posted: Tue Aug 08, 2006 2:58 pm
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.
Posted: Tue Aug 08, 2006 3:21 pm
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/>';
}
Posted: Tue Aug 08, 2006 10:34 pm
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.

Posted: Tue Aug 08, 2006 10:37 pm
by RobertGonzalez
Do you absolutely need to use cookies to do what you are doing?
Posted: Tue Aug 08, 2006 11:00 pm
by fitchic77
no. I'm trying it out with Session vars.
Posted: Tue Aug 08, 2006 11:16 pm
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
Posted: Tue Aug 08, 2006 11:44 pm
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?

Posted: Tue Aug 08, 2006 11:58 pm
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");
}

Posted: Wed Aug 09, 2006 12:03 am
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.
Posted: Wed Aug 09, 2006 12:05 am
by RobertGonzalez
No way is Jenk the best. It has to be me.
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;
?>
Posted: Wed Aug 09, 2006 12:08 am
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;