I am in the process of developing an online store. I am working on the shopping cart right now. The problem I am having is that sometimes when I add the first item to the cart it doesn't always recognize it. Then with each subsequent product added it will only recognize that one product. In other words it will only display the last product that was added. Now the keyword here is
sometimes. There are times when it will work flawlessly. The sequence of events in which it works and doesn't work are exactly the same. In both instances I have gone through the exact same routine.
Here is the code for adding an item to the cart:
PHP Code:
session_start();
if (!(isset($_SESSION['prod_id']))){
$_SESSION['prod_id'] = array($_POST['prod_id']);
$_SESSION['name'] = array($_POST['name']);
$_SESSION['reg_price'] = array($_POST['reg_price']);
$_SESSION['mem_price'] = array($_POST['mem_price']);
$_SESSION['quantity'] = array($_POST['quantity']);
session_write_close();
header("Location: http://www.kentuckygear.com/showcart.php");
end;
}
else{
array_push($_SESSION['prod_id'], $_POST['prod_id']);
array_push($_SESSION['name'], $_POST['name']);
array_push($_SESSION['reg_price'], $_POST['reg_price']);
array_push($_SESSION['mem_price'], $_POST['mem_price']);
array_push($_SESSION['quantity'], $_POST['quantity']);
session_write_close();
header("Location: http://www.kentuckygear.com/showcart.php");
end;
}
Here is the code used in showing the shopping cart:
*note: I am not showing the code that says "session_start();" but rest assured it is at the top of the page before anything else*
PHP Code:
if (isset($_SESSION['prod_id'])){
$subtotal = 0;
//not empty, loop through arrays and print each product's information
for($x=0; $x < sizeof($_SESSION['prod_id']); $x++){
echo <<<DONE
<tr><form action="updatequantity.php" method="post">
<td><p>{$_SESSION['prod_id'][$x]}</p></td>
<td><p>{$_SESSION['name'][$x]}</p></td>
<td><input type="text" size="2" name="quantity" value="{$_SESSION['quantity'][$x]}"></td>
DONE;
//if they are a member then apply discount
if (!isset($_SESSION['member'])){
echo <<<DONE
<td align="right"><p>\${$_SESSION['reg_price'][$x]}</p></td>
DONE;
//if not a member calculate regular price total for each product based on quantity for each
$regpricetotal= $_SESSION['reg_price'][$x] * $_SESSION['quantity'][$x];
//format price as a decimal
$regpricetotal= number_format($regpricetotal, 2, ".",",");
//increment subtotal
$subtotal += $regpricetotal;
echo <<<DONE
<td align=right><p>$$regpricetotal</p></td>
<td><input type="submit" value="Update"></td>
<input type="hidden" name="prod_id" value="{$_SESSION['prod_id'][$x]}">
</form>
</tr>
DONE;
}
//user is a valid member
else {
echo <<<DONE
//quote the member price instead of the regular price
<td align="right"><p>\${$_SESSION['mem_price'][$x]}</p></td>
DONE;
//calculate member price total for each product based on quantity ordered for each
$mempricetotal = $_SESSION['mem_price'][$x] * $_SESSION['quantity'][$x];
//format price as a decimal
$mempricetotal= number_format($mempricetotal, 2, ".",",");
//increment subtotal
$subtotal += $mempricetotal;
echo <<<DONE
<td align=right><p>\${$mempricetotal}</p></td>
<td><input type="submit" value="Update"></td>
<input type="hidden" name="prod_id" value="{$_SESSION[prod_id][$x]}">
</form>
</tr>
DONE;
}
}
//format subtotal as a decimal
$subtotal = number_format($subtotal, 2, ".", ",");
echo "<tr><th colspan=\"4\" align=\"right\">Subtotal:</th><td align=\"right\"><p>\${$subtotal}</p></td></tr>\n";
echo <<<DONE
DONE;
}
//shopping cart is empty (there are no items in Session arrays)
else{
echo "<p align=\"center\">There are currently no products in your shopping cart</p>";
}
echo <<<DONE
</table><font face="Arial" size="1" color="red">Notice: to delete a product from your cart,
set its quantity to 0 and click its update button</font><br><br>
<table border="0" align="center" cellpadding="5" cellspacing="5">
DONE;
//user has not entered there member number
//display a prompt to do so
if (!isset($_SESSION['member'])){
echo <<<DONE
<tr>
<td colspan="2">
<form action="checkmember.php" method="post">
<p>Members of the UK Alumni Association recieve a discounted price on select merchandise<br>
Enter your member ID number here to recieve your discount: <input type="text" size="15" name="id" value=""><br>
<input type="submit" value="Submit"> <font face="Arial" size="1" color="black">If you are not a member and would like to join, <a href="http://www.ukalumni.net/membership/join.htm" target="_blank">click here</a></font>
</form>
</td>
</tr>
DONE;
}
//user has entered member number
//tell them that they have member discount priviledges
else {
echo <<<DONE
<tr>
<td colspan="2">
<p>You have been verified as a member and you qualify for member discounts</p>
</td>
</tr>
DONE;
}
//there is an item in the shopping cart
//allow them to check out or empty there cart
if (isset($_SESSION['prod_id'])){
echo <<<DONE
<tr>
<td width="20%">
<form action="empty.php" method="post">
<input type="submit" value="Empty Shopping Cart">
</form>
</td>
<td>
<form action="checkout.php" method="post">
<input type="submit" value="Make Purchase">
</form>
</td>
</tr>
</table>
DONE;
}
I appreciate any help you all can give me.