I am looking forward to post the selected product in to the cart , Its happening but , but he selected option is not posted instead the first element is posted again. I have got different sizes of the product , and the price is dynamically generated once the desired size is selected. But the selected option is not getting it the cart instead the first element is going . please refer to the codes below :
- Product.php
[php]
<?php
//error reporting
error_reporting(E_ALL);
ini_set('display_errors','1');
?>
<?php
// Checking to see the url variable is set and that it exists in the database
if(isset($_GET['id'],$_GET['subcategory'])){
$id=$_GET['id'];
//$category=$_GET['category'];
$subcategory=$_GET['subcategory'];
$dynamicList="";
$dynamicProduct="";
$stockCount="";
include "scripts/connect_to_mysql.php";
$sql=mysql_query("SELECT * FROM newproducts WHERE id='$id' LIMIT 1");
$sql1=mysql_query("SELECT * FROM newproducts WHERE subcategory='$subcategory'");
$productCount=mysql_num_rows($sql);
$productCount1=mysql_num_rows($sql1);
if($productCount1>0){
$i=0;
while($row=mysql_fetch_array($sql1)){
$id=$row["id"];
$size=$row["size"];
$price_retail=$row["price_retail"];
$dynamicList.=''.$size.'';
}
$i++;
}else{
echo 'We have got nothing';
}
$productCount=mysql_num_rows($sql);
if($productCount>0){
//get all the product details
while($row=mysql_fetch_array($sql)){
$id=$row["id"];
$subcategory=$row["subcategory"];
$details=$row["details"];
$dynamicProduct='
|
Product Title:'.$subcategory.'
Details:'.$details.'
|
';
}
}else{
echo "No product Available with that ID";
exit();
}
}else{
echo "No product Available with that ID";
exit();
}
mysql_close();
?>
|
Product Title:
|
<?php echo $subcategory;?>
|
Details:
|
<?php echo $details;?>
|
Available sizes: |
<?php echo $dynamicList;?>
|
Price:
|
|
Stock Availability:
|
|
|
|
|
</table>
[/php]
2.Cart.php
[php]
<?php
session_start();//start session
//error reporting
error_reporting(E_ALL);
ini_set('display_errors','1');
//connecting to database
include "scripts/connect_to_mysql.php";
?>
<?php
if(isset($_POST['pid'])){
$pid=$_POST['pid'];
$size=$_POST['productSize'];
$wasFound=false;
$i=0;
//if cart session variable is not set or array is empty
if(!isset($_SESSION["cart_array"])|| count($_SESSION["cart_array"])<1){
$_SESSION["cart_array"]=array(1=>array("item_id" =>$pid,"size"=>$size,"quantity" =>1));
}else{
//run if the cart has at least one item in it
foreach($_SESSION["cart_array"] as $each_item){
$i++;
while(list($key,$value)=each($each_item)){
if ($key == "item_id"&&"size" && $value == $pid&&$size) {
//that items is in cart already so lets adjust
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "size" => $size, "quantity" => $each_item['quantity'] + 1)));
$wasFound=true;
}
}
}
if($wasFound==false){
//array_push($_SESSION["cart_array"],array("item_id" =>$pid,"quantity" =>1));
array_push($_SESSION["cart_array"], array("item_id" => $pid,"size" => $size, "quantity" => 1));
}
}
header("location:cart.php");
exit();
}
?>
<?php
//Render the cart for the user to view
//mysql query to get the product details
$cartOutput="";
$cartTotal="";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"])<1){
$cartOutput="Your Shopping cart is empty";
}else{
$i=0;
foreach($_SESSION["cart_array"]as $each_item){
$item_id=$each_item['item_id'];
$sql=mysql_query("SELECT * FROM newproducts WHERE id='$item_id' LIMIT 1");
while($row=mysql_fetch_array($sql)){
$subcategory=$row["subcategory"];
$details=$row["details"];
$category=$row["category"];
$price=$row["price_retail"];
$size=$row["size"];
}
$priceTotal=$price*$each_item['quantity'];
$cartTotal=$priceTotal+$cartTotal;
//Dynamic table row assembly
$cartOutput.='';
$cartOutput.='
'.$subcategory.''.'
| ';
$cartOutput.=''.$details.' | ';
$cartOutput.=''.$size.' | ';
$cartOutput.=''.$price.' | ';
$cartOutput.='
| ';
$cartOutput.=''.$priceTotal.' | ';
$cartOutput.=' | ';
$cartOutput.='
';
$i++;
}
}
?>
Your Cart
<br/><br/>
<table border="1" cellpadding="6" width="100%">
<tr>
<td width="18%">Product</td>
<td width="37%">Product Description</td>
<td width="10%">Selected Size</td>
<td width="10%">Price Per Unit</td>
<td width="9%">Quantity</td>
<td width="7%">Total</td>
<td width="9%">Remove</td>
</tr>
<?php echo $cartOutput;?>
</table>
<div align="right">
<?php echo "CartTotal : £".$cartTotal."GBP" ;?><br/>
</div>
<a href="cart.php?cmd=emptycart">Click here to empty your cart</a>
</div>
<br/>
</div>
</body>
[/php]