Reputation: 19
First of all, i am sorry if this question has been asked before.I am working on a shopping cart. My current code will only show 1 item in the cart. It means that when i click add to cart on #product1 it will show me #product1 detail in the cart. However when i click add to cart for #product2, it will override the information of #product1.
Here is my code for product:
<div >
<image src="ip5s.jpg">
<p><font color="blue">  Iphone 5S</font></p>
<p><font color="red">  RM1999</font></p>
<p><form name="addcart" method="post" action="processcart.php">
<input type="submit" name="addtocart" value="Add to cart">
<input type="hidden" name="product_id" value="1234" />
<input type="hidden" name="quantity" value="1" />
</form>
</p>
</div>
Here is the code for process.php:
<?php
session_start();
include_once("config.php");
$pid=$_POST['product_id'];
$_SESSION['product'] = array($pid);
sleep(2);
echo "Add to cart successful";
header("refresh:1;url=cart.php");
exit();
?>
Here is the code for cart.php:
<?php
if(!isset($_SESSION['cart']))
echo "<p>Your shopping cart is empty!</p>";
else{
$tblname="products";
require_once("dbcon.php");
$proids = array();
foreach($_SESSION['product'] as $id)
{
$proids[] = $id;
}
$proids = implode(',', $proids);
$query = "SELECT * from $tblname where product_id in ('$proids') ";
$result = mysql_query($query) or die(mysql_error());
echo "<table>";
while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
echo "<tr><td>" . $row[$i]['product_name'] . "</td><td>" . $row[$i]['product_price'] . "</td></tr>" ;
}
echo "</table>";
mysql_free_result($result);
mysql_close();
}
Upvotes: 1
Views: 351
Reputation: 1229
As you seem to be really stuck on this:
process_cart.php
<?php
session_start();
include_once("config.php");
// test & generate cart if needed
if(! isset($_SESSION['cart']))
{
$_SESSION['cart'] = array("products"=>array());
}
// add current product
// ! note: you need to add data validation to this to avoid SQL injects
$_SESSION['products'][$_POST['product_id']] = $_POST['quantity'];
// Show you have done something
sleep(2);
echo "Add to cart successful";
header("refresh:1;url=cart.php");
exit();
?>
databaseConnection.php
<?php
define("DB_HOST", "localhost");
define("DB_NAME", "onlinestore");
define("DB_USER", "root");
define("DB_PASSWORD", "");
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_NAME);
?>
cart.php:
<?php
session_start();
if (!isset($_SESSION['cart']))
{
// No items yet added to the cart
echo "<p>Your shopping cart is empty!</p>";
}
else
{
// set table
$tblname = "products";
// get connection
require_once('DatabaseConnection.php');
// create a list with product ID's
$prodids = array();
// For each product in the cart
foreach($_SESSION['cart']['products'] as $id => $qntity)
{
// add id to the array
$prodids[] = $id;
}
// combine into a string
$prodids = implode(',', $prodids);
$query = "SELECT * from $tblname where product_id in ($prodids) ";
// (The query should really be mysqli)
$result = mysql_query($query) or die(mysql_error());
echo "<table>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<tr><td>" . $row['product_name'] . "</td><td>" . $row['product_price'] . "</td></tr>". $_SESSION['cart']['products'][$row['product_id']];
}
echo "</table>";
mysql_free_result($result);
mysql_close();
?>
Upvotes: 0
Reputation: 6887
USE array_push()
.make an array with card Ids and display them using forloop
<?php
session_start();
include_once("config.php");
$pid = $_POST['product_id'];
$_SESSION['product'] = array();
$_SESSION['product'] = array_push($_SESSION['product'], $pid);
sleep(2);
echo "Add to cart successful";
header("refresh:1;url=cart.php");
exit();
?>
Try
<?php
session_start();
if(isset($_SESSION['quantity']))
$_SESSION['quantity']=$_SESSION['quantity']+1;
else
$_SESSION['quantity']=1;
echo "Cart=". $_SESSION['quantity'];
?>
Upvotes: 0
Reputation: 2211
Add session_start()
to the beginning of cart.php
to resume the session.
Also, in order to add multiple product IDs to the $_SESSION['product']
variable, which is probably what you'll be doing, you'll have to change
$_SESSION['product'] = array($pid);
to
$_SESSION['product'][] = $pid;
Which inserts $pid
to the end of $_SESSION['product']
array.
Upvotes: 1