I am working on a simple php shopping cart and I have run into a big problem. The products being offered are supposed to be identified by the engraving on the product and instead I have the product lookup done by product id.
I am not sure how to modify the code ot get the shopping cart to be brought up by engraving and not product id.
Here is the code to add something to the shopping cart:
This is the actual shopping cart viewing page:
I am not sure how to modify the code ot get the shopping cart to be brought up by engraving and not product id.
Here is the code to add something to the shopping cart:
Code:
<?php
session_start();
require_once('includes/db.php');
if (is_numeric ($_GET['pid'])) { // Check for a product ID.
$pid = $_GET['pid'];
// Check if the cart already contains one of these products.
if (isset($_SESSION['cart'][$pid]) && $_SESSION['eng'][$pid] && $_SESSION['eng'][$pid][0]) {
$qty = $_SESSION['cart'][$pid] + 1;
} else {
$qty = 1;
}
if(isset($_POST['qty'])){
$qty+=$_POST['qty'] - 1;
}
if(isset($_POST['engrave'])){
$engrave=$_POST['engrave'];
$date=date('Y-m-d');
$engQ=mysql_query("INSERT into product_initials values('','$pid','$engrave','','$date')")or die("Engrave enter query failed ".mysql_error()."<br>".$engQ);
$engQres=mysql_query("SELECT * FROM product_initials WHERE product_id='$pid' ORDER BY initial_id ASC")or die("Enrave return Query Failed ".mysql_error());
while ($row = mysql_fetch_array ($engQres, MYSQL_ASSOC)){
}
}
// Add to the cart session variable.
$_SESSION['cart'][$pid] = $qty;
$_SESSION['eng'][$pid] = $engrave;
//Route to Shopping cart view
header("Location: view_cart.html");
} else { // Redirect
header ("Location: [URL unfurl="true"]http://"[/URL] . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/index.html");
exit();
}
?>
This is the actual shopping cart viewing page:
Code:
<?php
// Check if the form has been submitted (to update the cart).
if (isset ($_POST['submit'])) {
foreach ($_POST['qty'] as $key => $value) {
if ( ($value == 0) AND (is_numeric ($value)) ) {
unset ($_SESSION['cart'][$key]);
} elseif ( is_numeric ($value) AND ($value > 0) ) {
$_SESSION['cart'][$key] = $value;
}
}
}
//check if "remove" checkbox selected and remove followthrough
if(!empty($_POST['rem']) && !empty($_POST['remcode'])){
unset($_SESSION['cart'][$rem]);
unset($_SESSION['eng'][$rem]);
//unset ($_SESSION['cart'][$_POST['remcode']]);
}else{
}
// Check if the shopping cart is empty.
$empty = TRUE;
if (isset ($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $key => $value) {
if (isset($value)) {
$empty = FALSE;
}
}
}
// Display the cart if it's not empty.
if (!$empty) {
// Retrieve all of the information for the products in the cart.
$query = 'SELECT * FROM catagories, products WHERE catagories.catagory_id = products.catagory_id AND products.product_id IN (';
foreach ($_SESSION['cart'] as $key => $value) {
$query .= $key . ',';
}
$query = substr ($query, 0, -1) . ') ORDER BY catagories.first_name ASC';
$result = mysql_query ($query);
// Create a table and a form.
echo '<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="left" width="30%"><b>Catagory</b></td>
<td align="left" width="30%"><b>Product Name</b></td>
<td align="left" width="5%"><b>Engrave</b></td>
<td align="right" width="10%"><b>Price</b></td>
<td align="center" width="10%"><b>Qty</b></td>
<td align="right" width="5%"><b>Subtotal</b></td>
<td align="left" width="5%"><b><font size="1">Remove</font></b></td>
</tr>
<form action="view_cart.html" method="post">';
// Print each item.
$total = 0; // Total cost of the order.
$countrem=1;
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
// Calculate the total and sub-totals.
$subtotal = $_SESSION['cart'][$row['product_id']] * $row['price'];
$total += $subtotal;
// Print the row.
echo " <tr>
<td align=\"left\">{$row['first_name']}</td>
<td align=\"left\">{$row['product_name']}</td>
<td align=\"left\">{$_SESSION['eng'][$row['product_id']]}</td>
<td align=\"right\">\${$row['price']}</td>
<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['product_id']}]\" value=\"{$_SESSION['cart'][$row['product_id']]}\" /></td>
<td align=\"right\">$" . number_format ($subtotal, 2) . "</td>
<td align=\"left\"><input type=\"checkbox\" name=\"rem\" value=\"{$row['product_id']}\">
<input type=\"hidden\" name=\"remcode\" value=\"{$countrem}\"></td>
</tr>\n";
$countrem++;
} // End of the WHILE loop.
// Print the footer, close the table, and the form.
echo ' <tr>
<td colspan="4" align="right"><b>Total:<b></td>
<td align="right">$' . number_format ($total, 2) . '</td>
</tr>
</table><div align="center"><input type="submit" name="submit" value="Update My Cart" /></form><br /><br /><a href="checkout.html"><font size="+3">Checkout</font></a></div>';
mysql_close(); // Close the database connection.
} else {
echo '<p><font size="-2"><b>STATUS:</b></font> Your cart is currently empty.</p>';
}
?>