Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem identifying unique products in shopping cart

Status
Not open for further replies.

s0crates9

Technical User
Jun 18, 2005
70
US
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:
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>';
}

?>
 
I don't know what information you track with an engraving, but I don't think I would assume that engraving is unique. I mean, think about Mother's Day -- a lot of people will be getting engraving with "Happy Mother's Day" on it.

I would, when a new engraving is entered into the system, record the engraving in a database and assign the engraving a unique ID at that time. Since your database backend is MySQL, using an auto_increment column in the table and mysql_insert_id() will be enought to provide you with a unique ID for any engraving.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Yes, I agree.
Either, have one field for engraving, with unique ID, or use the item_id as the unique_id.

eg:

table1:
item_id
item_cat_id_cat_id
item_desc
item_price
item_pic_id
item_engraving_id

table2:
engraving_id
engraving_title
engraving_pic_id_pic_id

table3:
item_cat_id
item_cat_name
item_cat_pic_id_pic_id

Then, SELECT * from table1, table2 WHERE item_engraving_id = engraving_id GROUP BY engraving_id;

Or:
SELECT * from table1, table3 WHERE item_cat_id_cat_id = item_cat_id GROUP BY item_cat_id;

ps. you might want to join all tables with natural join, as seen here, and do the field1 = field2, on all tables and then group on what you think is o.k.

grouping on engravement might be good, but so might also grouping on product_cat_id_cat_id be.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top