Good morning all. =)
I have a menu script that creates a list of menu items from my product_category table: category_id, category_name, category_parent
And then I have a product list script that is supposed to pull items from a given category when it is clicked on the menu.
The menu works as expected. It pulls the categories and lists them, and when the item is clicked is changes the url to that category.
My question is how do I get them to talk to each other, and how do I structure the query to do this.
Content of menu.php :
<?php
// Select all entries from the menu table
$result=mysql_query("SELECT DISTINCT category_id, category_name, category_parent FROM product_category ORDER BY category_name");
// Create a multidimensional array to conatin a list of items and parents
$menuData = array(
'items' => array(),
'parents' => array()
);
// Builds the array lists with data from the menu table
while ($menuItem = mysql_fetch_assoc($result))
{
// Creates entry into items array with current menu item id ie. $menuData['items'][1]
$menuData['items'][$menuItem['category_id']] = $menuItem;
// Creates entry into parents array. Parents array contains a list of all items with children
$menuData['parents'][$menuItem['category_parent']][] = $menuItem['category_id'];
}
// Menu builder function, parentId 0 is the root
function buildMenu($parent, $menuData)
{
$html = "";
if (isset($menuData['parents'][$parent]))
{
$html .= "
<ul>\n";
foreach ($menuData['parents'][$parent] as $itemId)
{
if(!isset($menuData['parents'][$itemId]))
{
$html .= "<li>\n <a href='category_filter_test.php?category=".$menuData['items'][$itemId]['category_name']."'>".$menuData['items'][$itemId]['category_name']."</a>\n</li> \n";
}
if(isset($menuData['parents'][$itemId]))
{
$html .= "
<li>\n <a href='category_filter_test.php?category=".$menuData['items'][$itemId]['category_name']."'>".$menuData['items'][$itemId]['category_name']."</a> \n";
$html .= buildMenu($itemId, $menuData);
$html .= "</li> \n";
}
}
$html .= "</ul> \n";
}
return $html;
}
echo buildMenu(0, $menuData);
?>
And content of product_listing.php
<?php
$menuData = $_GET['$menuData'];
// this creates the table data from the form selection in theory
// build and execute the query
if (isset($menuData)) {
$select = "SELECT * FROM product_category, products WHERE product_category.category_name = products.product_category AND products.product_category = \"$menuData\"";
$rslt = mysql_query($select) or die(mysql_error());
//loop through the results
//get each element and put it in a variable
if (!$rslt) {
exit(mysql_error());
} else {
echo <<<END
<table border="1" cellpadding="5">
<tr>
<th>Product ID</th>
<th>Product Thumbnail</th>
<th>Product Name</th>
<th>Product Short Description</th>
<th>Product Price</th>
<th>Add To Cart</th>
</tr>
END;
while ($row = mysql_fetch_array($rslt)) {
$id = $row['product_id'];
$thumbnail = $row['product_thumbnail'];
$name = $row['product_name'];
$short_desc = $row['product_short_desc'];
$price = $row['product_price'];
$paypal = $row['product_paypal'];
//Print out the results
echo <<<END
<TR>
<TD>$id</TD>
<TD>$thumbnail</TD>
<TD>$name</TD>
<TD>$short_desc</TD>
<TD>$price</TD>
<TD>$paypal</TD>
</TR>
END;
}
}
}
echo "</TABLE>";
?>
I know that $menuData is not the right variable to be passing, but I do not know what variable it needs. Do I need to add something to menu to achieve this? can I even do this with the $_GET? I thought I read that you could.
I think that is all of my questions. lol. Have a great day, and thanks so much for you help now and in the past day. =)
Kate