on my shopping cart i create a new session "basket" for every user.
when they select an product to add to there basket, i add that products "id" & "quantity" to the session like below:
<?php
session_start();
if(!isset($_SESSION['basket']))
{
$_SESSION['basket'] = array();
}
if(isset($_POST['selected_item_id']))
{
$u_item_id = $_POST['selected_item_id'];
$u_quantity = $_POST['sel_item_qty'];
$u_details = $u_item_id."|".$u_quantity;
$_SESSION['basket'][] = $u_details;
header("Location: showcart.php");
}
?>
in showcart.php I get the "id" and "quantity" from the session by:
$product = $_SESSION['basket'][$i];
$myarray = explode('|', $product);
this code is inside a for loop as for the reason of the "$i" been used....
my problem lies in selecting the products colour from tblstore_colour where the tblstore_colour FK "item_id" is equals to the tblstore_items PK "$myarray[0]"
i get the same colour for each of my products when only some products in my basket have a colour.
this is how i query tblstore_colour for each product colour:
$get_color = "SELECT * FROM tblstore_colour, tblstore_items WHERE tblstore_colour.item_id = tblstore_items.id AND tblstore_colour.id = $myarray[0]";
querying the tblstore_items for products details where tblstore_items.id = $myarray[0] works fine, displaying each products details in a table but i have colours in a separate table and this is where my problem lies...
here is how i have my table created/populated:
mysql_query("insert into tblstore_items values ('1', '1', 'Easter Egg Selection',5.99, 'Which chocolate most tickles your fancy this easter?','images/easter/newsletter_easter.jpg')");
mysql_query("insert into tblstore_items values ('2', '1', 'Chocolate Bunny',4.99, 'Give this gorgeous bunny to a loved one this easter.','images/easter/EasterBunny.jpg')");
mysql_query("insert into tblstore_items values ('3', '1', 'Easter Candle Pack',10.99, 'Light up your life this easter with some scented candles.','images/easter/eastdisp.jpg')");
mysql_query("insert into tblstore_colour values ('1', '2', 'Gold')");
mysql_query("insert into tblstore_colour values ('2', '2', 'Silver')");
mysql_query("insert into tblstore_colour values ('3', '3', 'Yellow')");
mysql_query("insert into tblstore_colour values ('4', '3', 'White')");
mysql_query("insert into tblstore_colour values ('5', '3', 'Mixed')");
again tblstore_colour has PK and FK, FK been tblstore_item's PK...
so again my code to loop through each product in the basket and display its details ina table is:
for($i = 0; $i < count($_SESSION['basket']); $i++)
{
$product = $_SESSION['basket'][$i];
$myarray = explode('|', $product);
$get_cart = "SELECT c.id AS cat_id, c.cat_title, si.id, si.item_title, si.item_price, si.item_desc, si.item_image FROM tblstore_items AS si LEFT JOIN tblstore_categories AS c ON c.id = si.cat_id WHERE si.id = $myarray[0]";
$get_cart_res = mysql_query($get_cart,$conn);
while($row = mysql_fetch_array($get_cart_res))
{
$item_id = $row['id'];
$item_title = $row['item_title'];
$item_price = $row['item_price'];
}
$unit_price = sprintf("%.02f", $item_price * $myarray[1]);
$total_price += $unit_price;
$get_color = "SELECT * FROM tblstore_colour, tblstore_items WHERE tblstore_colour.item_id = tblstore_items.id AND tblstore_colour.id = $myarray[0]";
$get_color_results = mysql_query($get_color,$conn);
while($row_colour = mysql_fetch_array($get_color_results))
{
$colour = $row_colour['item_colour'];
}
$colours = (empty($colour)) ? " " : $colour;
$display_block2 .= "
<tr>
<td class=border align=center>$item_title</td>
<td class=border align = center>$size</td>
<td class=border align = center>$colours</td>
<td class=border align = center>£ $item_price</td>
<td class=border align = center>$myarray[1]</td>
<td class=border align = center>£ $unit_price</td>
<td class=border align = center><a href=removefromcart.php?id=$item_id>Remove</a></td>
</tr>";
}
thanks for help in advance...
if you require more information please ask!
paul_mellon36 :D