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!
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]
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.
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.
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.
<?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.
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?
Last edited by fitchic77 on Sat Oct 16, 2010 11:36 am, edited 1 time in total.
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...