How to create Dynamic Menu using PHP

Asked By 400 points N/A Posted on -
qa-featured

I’m making my own personal site for my portfolio. I’ve already created my mock-up design for the site. Slice all the necessary images and convert it into HTML and CSS code. Now, I want to create a dynamic menu which I can modify the menu, add more menu and submenu, i think this is possible but I don’t know how to code this. I’m new to PHP and I need some expert to provide me sample PHP script on how to do this. Kindly also include how to make a database for this. This is going to be a great challenge for me and I need someone’s help.

SHARE
Answered By 0 points N/A #108218

How to create Dynamic Menu using PHP

qa-featured

Hi Annissa,

At first you need the following database:
 
1. MySQL Database.
 
2. PHP.
 
You also need to be comfortable with adding, creating, modifying and deleting records in a database table.
 
In order to connect to the database, add the following code between the opening and closing.
<Body> tags:
 
<? PHP
//-Connect to our database
$db=mysql_connect("localhost", "<username>", "<password>") or die
('Can't connect to the database because: ') . mysql_error();
//-Select the database that we want to use
mysql_select_db("MyDatabase");
?>
 
In order to inform PHP where the data is coming from, from the following SQL statement right above the closing PHP block:
 
$query="SELECT FileName, Description FROM Navigation";
 
Since the database table could have hundreds, possibly thousands of records we need to create the links inside a loop, specifically a while loop. The while loop is added below:
 
While ($row=mysql_fetch_array ($result))
{
$FileName=$row['FileName'];
$Description=$row['Description'];
//-Display the results of the query in HTML format using paragraphs
Print "<p><a href="".$FileName."/".Description.</a></p>";
}
 
 
Hope this will work.

Related Questions