Reputation: 45
I am trying to make a shopping cart in php using sessions. I am having trouble making it so when you add more than one item to the shopping cart, that it will display ALL items that are added to the cart. As of right now I have it so only one thing can be in a shopping cart and when you leave the page or add another item, the last item gets overwritten. I know I must add a session like $_SESSION['cart'] and save the information in there when it gets printed out but I am unsure how to do that. Any help would be appreciated.
$prodid=$_GET['pid'];
$quan=$_GET['quantity'];
$query="select * from Products where ProductID = '$prodid'";
$result=mysql_query($query);
$numOfRows=mysql_numrows($result);
for($i=0;$i<$numOfRows;$i++)
{
$productID=mysql_result($result, $i, "ProductID");
$prodTitle=mysql_result($result, $i, "Title");
$prodAuthor=mysql_result($result, $i, "Author1");
$prodPrice=mysql_result($result, $i, "Price");
Print"<h4>ID: $productID \n </h4>";
Print"<h4>Title: $prodTitle \n </h4>";
Print"<h4>Author: $prodAuthor \n </h4>";
Print"<h4>Price: $ $prodPrice \n </h4>";
}
EDIT:
$prodid=$_GET['pid'];
$quan=$_GET['quantity'];
Print"$prodid";
Print"<br/>";
Print"$quan";
$query="select * from Products where ProductID = '$prodid'";
$result=mysql_query($query);
$numOfRows=mysql_numrows($result);
for($i=0;$i<$numOfRows;$i++)
{
$productID=mysql_result($result, $i, "ProductID");
$prodTitle=mysql_result($result, $i, "Title");
$prodPrice=mysql_result($result, $i, "Price");
}
$arr = array(
['id'] => "$productID",
['title'] => "$prodTitle",
['count'] => "$quan",
['price'] => "$prodPrice"
);
echo "id is " . $arr['id'];
Upvotes: 0
Views: 399
Reputation: 5532
One of the best ways for shopping cart is to use database table. 1) simply create a table cart (id, guest-id, product-id, date) 2) generate guest id and assign to session once guest is clicked add to cart
example:
if(isset($_GET['adtocart'])){
if(!isset($_SESSION['guest'])){
$ipaddress = $_SERVER['REMOTE_ADDR'];
$id = uniqid(rand());
$addnewguest = mysql_query("insert into guest values('$id', '$ipaddress', sysdate())");
if($addnewguest)
$_SESSION['guest'] = $id;
else
header('location: ../en/systemnotify.php?case=002');
}
$cartid = uniqid(rand());
$guestid = $_SESSION['guest'];
$productid = $_GET['adtocart'];
$getproduct = mysql_fetch_array(mysql_query("select * from product where id = '$productid'"));
$url = $getproduct['url'];
$title = mysql_real_escape_string($getproduct['title']);
$price = $getproduct['price'];
$image = $getproduct['image'];
$info = mysql_real_escape_string($getproduct['info']);
$cartid = uniqid(rand());
$addtocart = mysql_query("insert into cart values('$cartid', '$guestid', '$productid', '$url', '$title', '$price', '$image', '$info', sysdate()) ");
if($addtocart){
header('location: '.$_SERVER['HTTP_REFERER'].$gpath.'added');
}
else
print mysql_error();
}
Upvotes: 0
Reputation: 480
You could simply form an array with the necessary item details in it:
$arr = array(
['id'] => 123
['title'] => 'some title'
['count'] => 1
['price'] => 9.99
);
You then save that array to $_SESSION
with
$_SESSION['cart'] = $arr;
When you're ready to add another item to the cart, create another array with the same keys as $arr
- $newArr
- and append that array to the session variable:
$_SESSION['cart'][] = $newArr;
Now, when you're ready to read all the items in the cart, you have an associative array to read from:
for($x = 0; $x < count($_SESSION['cart']); $x++) {
echo $_SESSION['cart'][$x]['title'];
}
Upvotes: 1