I created a custom checkout/shopping cart system. I'm running into an issue though. Some of my products will have different options like colors or sizes. As of now, I only have one database table created called products with all of my information in it like product name, id, price, item details, description, etc. I display all of the products on one page and then if you click on the products link I grab that ID and show that products information.
Should I add a colors and size option to my products db or create new db tables?
Then how can I get these options to display ONLY if that product has different colors or sizes?
I'm adding products to my cart like this
<div class="addcartbox">
<div class="productitemwrapper">
Price: <span class="addtocartboxtotal"><?php echo "$" . $products[$product_id]['price']; ?></span>
<div class="productdetailquantity"><?php echo"<form action='./itemsadded.php?view_product=$product_id' method='POST'>"?>
<select class="productquantity" name='quantity'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
</select>
</div><br><br>
//I want to add the color and size options here
<div class="productdetailaddbutton">
<?php
echo "<input type='hidden' name='product_id' value='$product_id' />
<input type='submit' class='addtocart' name='add_to_cart' value='Add to cart' />";
?>
<br>
<?php
// Add Product to Cart
if(isset($_POST['add_to_cart'])) {
$product_id = $_POST['product_id'];
// If item is already in cart, tell user
if(isset($_SESSION['shopping_cart'][$product_id])) {
echo "Item already in cart!<br />";
}
// Otherwise add to cart
else {
$_SESSION['shopping_cart'][$product_id]['product_id'] = $_POST['product_id'];
$_SESSION['shopping_cart'][$product_id]['quantity'] = $_POST['quantity'];
echo $message = "Added to cart!";
}
}
?>
</form>
</div>
</div>
</div>
I want to add the color and size options in the location I commented in my code. I want to echo out the info from the db in option values. So this would mean I would have to have each color or size be a different row in the database table correct?
I'm not going to have many products, so this doesn't need scaled to a huge degree. I just more or less need this to work in the system I have set up, so I can continue displaying my products with a loop I created and only showing these fields if the product has different options with it.
UPDATE:
I based this on how I was displaying all of my products through a foreach loop. I'm not sure if I am even close...
<?php
//connection to db
$con = mysqli_connect("localhost", "root", "", "bfb");
//Check for errors
if (mysqli_connect_errno()) {
printf ("Connect failed: %s
", mysqli_connect_error());
exit();
}
//Fetch products
$products = array();
$colors = mysqli_query($con,"SELECT * FROM colors");
while($row = mysqli_fetch_array($colors)) {
$products[$row['product_id']] = $row;
}
// Loop to display all products
foreach($colors as $id => $color) { ?>
<select name='colors'>
<option value='1'><?php echo $color; ?></option>
</select>
<?php } ?>