How can I use a database and PHP sessions to store a user's shopping cart? I am using CodeIgniter, if that helps.
Example code would also be nice.
How can I use a database and PHP sessions to store a user's shopping cart? I am using CodeIgniter, if that helps.
Example code would also be nice.
I would recommend that you look at the CodeIgnitor Session Class.
Additionally, you could look at Chris Shiflett's discussion on this topic.
Hi, i am not familar with CodeIgniter, but i would write a add to basket funtion like this:
function AddToBasket(){
if(is_numeric($_GET["ID"])){
$ProductID=(int)$_GET["ID"];
$_SESSION["Basket"][]=$ProductID;
$sOut.=ShowBasketDetail();
return $sOut;
}
}
In this Shoping Basket funktion we save Product IDs in an Session array. Now here the Show Basket funktion:
function ShowBasket(){
foreach($_SESSION[Basket] as $ProductID){
$sql="select * from products where ProductID=$ProductID";
$result=mysql_query($sql);
$row=mysql_fetch_row($result);
echo "Product: ".$row[0];
}
}
Foreach ProudctID in our Session Basket we make a SQL query to output Product Informations...
Now last but not least a clear Basket function:
function ClearBasket(){
unset($_SESSION[Basket]);
}
Dont forget session_start();
bevor you add any Product IDs to your Session Basket.
Also dont forget the mysql_connect();
function, you need this bevore you make any query.
Best Regards
nfo codejungle.org
how about this ; - when guest add one item product in the cart
function addCartItem($item_id, $qty)
{
$basket = $this->session->userdata('basket');
if(!$basket)
{
$this->session->set_userdata('basket', array($item_id => $qty));
}
else
{
## get array from $basket and *merge some new value from input
}
}