Asked By
annissa
400 points
N/A
Posted on - 06/08/2011
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.
How to create Dynamic Menu using PHP
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.