Jump to content

Using Arrays to Create Menu


Ben2

Recommended Posts

Hello,

 

I hope someone can help me with this as I really cant seem to get it to work!

 

I am wanting to create a navigation menu from data on a SQL database table, I know how I need to present the data to create the menu but I can not get it to work.

 

Here is an example of what I need:

//get the top menu
$sql = "SELECT name, link FROM menu WHERE category = 'none'";
$result = mysql_query($sql,$conn);
while($row=mysql_fetch_assoc($result))
{
  $topmenu[] = $row['name'];
}

//go through the top menu names
foreach($topmenu as $top)
{
       //echo the current top meny name
echo $top."<br />\n";

//get menu name and link if it matched the current top menu name
$sql = "SELECT name, link FROM menu WHERE category = ".$top."";
$result2 = mysql_query($sql,$conn);
while($row2=mysql_fetch_assoc($result2))
{
                //show the menu name and link
	echo $row2['name']."<br />\n";
	echo $row2['link']."<br />\n";
}
     
        //create space between top menu names
        echo "<br /><br />";
}

 

This was my first attempt which did not work at all but since then I have tried multiple things such as putting all the data into multiple arrays, in to one array etc but I couldnt get anything to produce the result that I need.

 

what I am hoping to produce is something like:

 

Home

Information

    Info1

    Info2

    Info3

About us

    About us1

    About us1

etc

etc

 

Once I can produce the data in this way I can then add the html, css etc to create the menu.

 

I will be very greateful for any help and advice that you can provide.

 

Kind Regards

Ben

 

Link to comment
Share on other sites

cant seem to get it to work!

Means what, exactly?  Nothing displays? You get error message?

 

Thank you for the quick reply,

 

Sorry I forgot to include the error message I get from that code.

Request Topic
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /blah/blah/blah/blah.com/blah/blah/blah.php on line 27

Home
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /blah/blah/blah/blah.com/blah/blah/blah.php on line 27

Information
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /blah/blah/blah/blah.com/blah/blah/blah.php on line 27

Tips
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /blah/blah/blah/blah.com/blah/blah/blah.php on line 27

Opinions
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /blah/blah/blah/blah.com/blah/blah/blah.php on line 27

 

Line 27 is:

while($row2=mysql_fetch_assoc($result2))

 

Kind Regards

Ben

Link to comment
Share on other sites

Here's a useful tip: When using double quotes to enclose a string, there isn't any need to use string concatenation to include variables. The two strings below are functionally identical. There's a big difference in readability and potential for error though.

<?php
$field1 = 'name';
$field2 = 'address';
$table = 'users';
$array['index'] = 'Frank';
$num = 10;

$string1 = "SELECT `$field1`, `$field2` FROM `$table` WHERE `$field1` = '{$array['index']}' LIMIT $num";
// AND //
$string2 = "SELECT `" . $field1 . "`, `" . $field2 . "` FROM `" . $table . "` WHERE `" . $field1 . "` = '" . $array['index'] . "' LIMIT " . $num;

echo "<br>String1: $string1<br>String2: $string2";
?>
Returns:
String1: SELECT `name`, `address` FROM `users` WHERE `name` = 'Frank' LIMIT 10
String2: SELECT `name`, `address` FROM `users` WHERE `name` = 'Frank' LIMIT 10

 

EDIT: Added <?php tags for syntax highlighting . . .

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.