vanessia_1999 0 Junior Poster in Training

Hi, I have a coding problem. I know it is all over the place because I am confuse to what to do with it. The way that I had it in the first coding example if a customer chooses two items of the same product but in different colors my code doesn't take that into consideration. It over writes the same product information and just change the color instead of adding another line of the product with the different color. Now instead of one line it should be two line with the same products but different color, but it does not show that way. It over write only the product color:(

<?php

$prodid = $_POST['prodid'];
$quantity = $_POST['quantity'];

if (isset($_POST['item_color'])) 
{
$item_color = $_POST['item_color'];
if (!isset($_SESSION['item_color'])) 
{
 $_SESSION['item_color'] = array();
}
$_SESSION['item_color'][$prodid] = $item_color; 

$query = "SELECT quantity, description FROM products WHERE prodid = $prodid";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$stock = $row[0];
$description = $row[1];

if (isset($_SESSION['cart'][$prodid]))
{
$total = $_SESSION['cart'][$prodid] + $quantity;
if ($total > $stock)
{
echo "<h2>Sorry, there are only $stock $description left in stock</h2>\n";
echo "<h2>Please select another quantity</h2>\n";
} else
{
$_SESSION['cart'][$prodid] += $quantity;
echo "<h2>Product added to cart.</h2>\n";
}
} else
{
if ($quantity > $stock)
{
echo "<h2>Sorry, there are only $stock $description left in stock</h2>\n";
echo "<h2>Please select another quantity</h2>\n";
} else
{
$_SESSION['cart'][$prodid] = $quantity;
echo "<h2>Product added to cart.</h2>\n";
}
}
echo "<a href=\"index.php\">Continue shopping</a><br>\n";
echo "<a href=\"index.php?content=checkout\">Check out</a>\n";
?>

In this code below a friend of mines told me to add this into my code, but I don't get the placement of where to put it?

$_SESSION['cart']['prodid']['colorid'] = $quantity;
{
foreach ($_SESSION['cart'] as $prodid => $value)
{
foreach ($prodid as $colorid => $quantity)
{
echo "Product: $prodid, color: $colorid, Quantity: $quantity<br/>\n";
}

Therefore, I wrote it like this and continue coming up with parse error, plus it look weird it does not look right

<?php

$prodid = $_POST['prodid'];
$quantity = $_POST['quantity'];

if (isset($_POST['item_color'])) 
{
$item_color = $_POST['item_color'];
if (!isset($_SESSION['item_color'])) 
{
 $_SESSION['item_color'] = array();
}
$_SESSION['item_color'][$prodid] = $item_color; 


$_SESSION['cart']['prodid']['colorid'] = $quantity;
{
foreach ($_SESSION['cart'] as $prodid => $value)
{
foreach ($prodid as $colorid => $quantity)
{
echo "Product: $prodid, color: $colorid, Quantity: $quantity<br/>\n";
}

$query = "SELECT quantity, description FROM products WHERE prodid = $prodid";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$stock = $row[0];
$description = $row[1];

if (isset($_SESSION['cart'][$prodid]))
{
$total = $_SESSION['cart'][$prodid] + $quantity;
if ($total > $stock)
{
echo "<h2>Sorry, there are only $stock $description left in stock</h2>\n";
echo "<h2>Please select another quantity</h2>\n";
} else
{
$_SESSION['cart'][$prodid] += $quantity;
echo "<h2>Product added to cart.</h2>\n";
}
} else
{
if ($quantity > $stock)
{
echo "<h2>Sorry, there are only $stock $description left in stock</h2>\n";
echo "<h2>Please select another quantity</h2>\n";
} else
{
$_SESSION['cart'][$prodid] = $quantity;
echo "<h2>Product added to cart.</h2>\n";
}
}
echo "<a href=\"index.php\">Continue shopping</a><br>\n";
echo "<a href=\"index.php?content=checkout\">Check out</a>\n";
?>

My question and concern is, is there a way to write this page so that everytime a customer choose a product with an additional different color, a seperate line of the product with the new color will display?
Example:
Punk Hair - Red
Punk Hair - Blue

and not the blue replacing the red?

I really appreciate your time and help, thank you in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.