Okay, getting somewhere now. But something weird is happening. And it may be very simply and my tired head can't just see something obvious in front of me.
Let's say, for example, there are two items in the cart, and then on the mycart.php page, I iterate through the cart and print out various things into a table...each cart item gets its own table.
So if i had two items, i will get two tables, three items---three tables, and so forth.
The weird thing is that for any number of items in the cart (let's say that number is x), I get x+1 tables. So if there are 4 items, i get 5 tables. The first table does not priint out anything, because the value it gets from the $_SESSION array is apparently garbage. Then the next four tables are printing correctly.
Here's my code at the top of the page. This code basically checks to see how one came to this page. If $POST['type'] is set, that means that arrived at the page via posting an order/request form. So I first save that particular post to the $SESSION.
<?php
$prodID = $_POST['prodID'];
include('../global/dbinfo.php');
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $conn);
$result = mysql_query("SELECT * FROM products WHERE item_num='$prodID'", $conn);
if (!$result) {
echo 'No results from query: '.mysql_error();;
exit;
}
$myrow = mysql_fetch_array($result);
if (isset($_POST['type'])) {
if (isset($_SESSION['cart']['numitems'])) {
$_SESSION['cart']['numitems']+=1;
}
else {
$_SESSION['cart']['numitems'] = 1;
}
$index = $_SESSION['cart']['numitems'];
$type = $_POST['type'];
if ($type == "Order") {
$productDetails = array("type"=>$type,
"prodID"=>$prodID,
"qty"=>$_POST['qty'],
"pColor"=>$_POST['pColor'],
"IHdate"=>$_POST['IHdate'],
"textlayout"=>$_POST['textlayout'],
"textimprintcolor"=>$_POST['textimprintcolor'],
"textinfo"=>$_POST['textinfo'],
"logodescription"=>$_POST['logodescription'],
"logoimprintcolor"=>$_POST['logoimprintcolor'],
"textlayout"=>$_POST['textinfo'],
"artfile"=>$_POST['artfile'],
"addlnotes"=>$_POST['addlnotes']);
$_SESSION['cart'][$index] = $productDetails;
}
elseif ($type == "Information Request") {
$productDetails = array("type"=>$type,
"prodID"=>$prodID,
"addlnotes"=>$_POST['addlnotes']);
$_SESSION['cart'][$index] = $productDetails;
}
}
?>
Next, later down the page, I want to iterate through the cart IF the cart has contents. Here's that code:
<?php
// Check to see if the cart has anything at all
if (isset($_SESSION['cart']['numitems'])) {
// cart has stuff. So iterate through the cart array.
foreach ($_SESSION['cart'] as $productArray) {
// Get the prodID from the productArray and run a query on this prodID
$prodID = $productArray['prodID'];
$result = mysql_query("SELECT * FROM products WHERE item_num='$prodID'", $conn);
if (!$result) {
echo 'No results from query: '.mysql_error();;
exit;
}
// We only care about the first row, as there may be a couple of duplicates
$myrow = mysql_fetch_array($result);
echo '<table width="100%" cellpadding="0" cellspacing="0" bgcolor="#181EA5">';
echo ' <tr>';
echo ' <td width="100%">';
echo ' <table width="100%" cellpadding="" cellspacing="1">';
echo ' <tr>';
echo ' <td width="77%" valign="middle" align="left" class="content8" bgcolor="#FFFFFF">';
echo ' <table width="97%" align="center" border="0" cellpadding="0" cellspacing="0">';
echo ' <tr>';
echo ' <td class="content9b" colspan="2">' . $myrow['name'] . '<br><br></td>';
echo ' </tr>';
echo ' <tr>';
echo ' <td width="15%" class="content7">Item #: </td>';
echo ' <td width="85%" class="content7">' . $myrow['item_num'] . '</td>';
echo ' </tr>';
echo ' <tr>';
echo ' <td width="15%" class="content7">Notes: </td>';
echo ' <td width="85%" class="content7">' . $productArray['addlnotes'] . '</td>';
echo ' </tr>';
echo ' <tr>';
echo ' <td width="15%" class="content7">Type: </td>';
echo ' <td width="85%" class="content7">' . $productArray['type'] . '</td>';
echo ' </tr>';
echo ' </table>';
echo ' </td>';
echo ' <td width="23%" align="center" rowspan="2" bgcolor="#FFFFFF">';
echo ' <img src="/images/prod_cat/' . $myrow["catID"] . '/' . $myrow["subcatID"] . '/' . $myrow["item_num"] . 's.jpg"></td>';
echo ' </tr>';
echo ' <tr>';
echo ' <td bgcolor="#EEEEEE"> </td>';
echo ' </tr>';
echo ' </table>';
echo ' </td>';
echo ' </tr>';
echo '</table><br>';
}
}
else {
echo '<div align="center">';
echo '<table width="90%" cellpadding="1" cellspacing="0" bgcolor="#181EA5">';
echo ' <tr>';
echo ' <td>';
echo ' <table width="100%" cellpadding="7" cellspacing="0" bgcolor="#E2E1E1">';
echo ' <tr>';
echo ' <td align="center" class="content9b">Your cart is empty.</td>';
echo ' </tr>';
echo ' </table>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
echo '</div>';
}
?>
Please excuse the "newbie" use of echo's. Oh well. 😃
So if you are able to take a look at that code, and somehow weave your way through my (perhaps crazy) logic, why is the output showing an additional table? Is that something specific with the foreach instruction. Please know, I am very new at this, so maybe this is something in the foreach iteration that I am not aware of. But if there are only two items in $SESSION['cart'], then shouldn't the foreach loop only go two times? And if your answer is yes, then the other thing i tried to realize was that, okay, then that means that somehow I'm "adding" a set of blank data to $SESSION['cart'] before I add the first product. But I can't see how/where this is happening.
Hope this makes a bit of sense and one of you geniuses can help out.
Thanks.