Need some help with a PHP/MYSQL menu..

Hello,

I have a webshop and i am using mysql and PHP for the site.

I would like to make som adjustments to the menu and add a menu on top of the site with the categories and also the sub categories.

I would like it be something like this:
Category 1 Category 2
Sub cat 1 Sub cat 1
Sub cat 2 Sub cat 2

When holding mouse over category 1 or two the sub categories pops up under it.

I have tried to implement it with the superfish jquery menu, but could not get it to work.

Hope that someone can help with this… :slight_smile: Thanks in advance.

[php]

<?php function ShowMenu() { //Get the list of products $sqlstr = sprintf("SELECT * FROM shop_categories WHERE Parent='0' ORDER BY Sortorder ASC"); $result = GetResultsFromDB($sqlstr); echo ""; echo ""; while($row = mysql_fetch_array($result)) { echo ""; //Get all child categories $sqlstr_child = sprintf("SELECT * FROM shop_categories WHERE Parent='%s' ORDER BY Sortorder ASC", $row['Id']); $result_child = GetResultsFromDB($sqlstr_child); while($row_child = mysql_fetch_array($result_child)) { echo ""; } } echo "
Produkter
" . $row['Name'] . "
- ". $row_child['Name'] . "
"; } ?>

[/php]

Regards
Steinar Sandvik

Ok, so what I would do would be to use a CSS based menu. Heres a simple tutorial that will get you started.

https://www.servage.net/blog/2009/03/20/create-a-cool-css-based-drop-down-menu/

All you’ll have to do is use your PHP code to write the products you want in the list.

Post back if you get stuck.

Thanks, i will try this.

Do you have an idea how i should implement the php/mysql code inside the li/ul?
Just an example of where to put the start of mysql and where to put the import of products?

Regards,

Steinar Sandvik

Something like this.

[php]function ShowMenu() {

//Get the list of products
$sqlstr = sprintf("SELECT * FROM  shop_categories WHERE Parent='0' ORDER BY Sortorder ASC");
$result = GetResultsFromDB($sqlstr);
echo '<ul id="menu">';
while($row = mysql_fetch_array($result))
{
    echo '<li><a href="">Products</a><ul>';
	//Get all child categories
	$sqlstr_child = sprintf("SELECT * FROM  shop_categories WHERE Parent='%s' ORDER BY Sortorder ASC", $row['Id']);
    $result_child = GetResultsFromDB($sqlstr_child);
	while($row_child = mysql_fetch_array($result_child))
	{
    	echo "<li><a href=href=\"show_category.php?id=".$row_child['Id']."\">. $row_child['Name'] . </a></li>";
	}
	echo '</ul></li>';
	
}
echo "</ul>"; 

}[/php]

also i notice your using
[php]mysql_fetch_array[/php]
This works great, i dont like it so much because it gives you more data then you need for instance, lets say your result has 2 colums, your array has 4 parts because its returning both named and numbered elements.

example

if you used
[php]mysql_fetch_assoc[/php]
it would only return
YourArray(
[name] = > John
[lastname] Smith)

for big queries it makes a difference, uses less memory to store the data.

Great :smiley:
Thank you very much, i will try this and let you know if it works :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service