Hi, i am making a menu and want to be able to add menu links by simply adding a new record to a database, i have got the main concept going but am having some troubles, instead of writing: the same piece of code to load a new menu item several times how can i write 1 line of code and just get it to repeat for each record in the database? and how can i get the record load from the database if i do something like this?
Anny help is muchly appreciated.
dlannetts
0
Junior Poster in Training
Recommended Answers
Jump to PostYou can include a page with php... You could create a menu.html or menu.php depending on what will it have... and then don't copy and paste, include it...
include('menu.html');
That's simpler... i dont recomend you to write a database because all the time you want to update …
Jump to PostSuppose that you have a db named menudb, a table named menu, and it has name and href (columns). You should do this:
…<?php $con=mysql_connect("localhost","your_db_user","your_db_password") or die("Can't start connection !"); $db=mysql_select_db("menudb",$con) or die("Can't connect to db !"); $result=mysql_query('SELECT * FROM menu'); while($row=mysql_fetch_assoc($result)){ echo "<a href='$row['href']'><p>$row['name']</p></a>"; } ?>
Jump to Postdo this
echo "<a href='".$row['href']."'>".$row['name']."</a>";
Jump to PostDo an include because if you modify something you will have to modify all the .php files that have that code... And you are welcome !!! Is it working like you wanted ???
P.S.: Mark this thread as solved if now works !
Jump to PostThere's actually an alternative method to display HTML. Use the heredoc syntax:
while($row=mysql_fetch_assoc($result)){ echo <<<HTML <html><body> <div id="menu" <ul id="main_menu"> <li><a href="{$row['link']}">{$row['title']}</a></li> </ul> </div> </body></html> HTML; } Note: The last line(HTML;) CANNOT BE INDENTED. Note2: [code=html] <html><body> <div id="menu"
The div tag needs a >, e.g: <div …
All 20 Replies
P0lT10n
dlannetts
0
Junior Poster in Training
P0lT10n
dlannetts
0
Junior Poster in Training
P0lT10n
dlannetts
0
Junior Poster in Training
P0lT10n
dlannetts
0
Junior Poster in Training
P0lT10n
dlannetts
0
Junior Poster in Training
dlannetts
0
Junior Poster in Training
P0lT10n
dlannetts
0
Junior Poster in Training
Lsmjudoka
0
Junior Poster in Training
dlannetts
0
Junior Poster in Training
Lsmjudoka
0
Junior Poster in Training
dlannetts
0
Junior Poster in Training
dlannetts
0
Junior Poster in Training
Lsmjudoka
0
Junior Poster in Training
dlannetts
0
Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.