Laoding Menu Links from a DB with PHP
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
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0
thank you for your response, i am making this for people that cant read website languages. So would you happen to know how to do it the way i wanted to?
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\wamp\www\edit site\php_files\main_menu.php on line 13
in your code its this line:
echo "<a href='$row'><p>$row</p></a>";
I have never seen this one before, what do i do?
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0
Ok, i am having another problem with the code but before i go on, if i was to make something like this, would i be best to load all the php from separate files and just load it up from an index.php, because right now, i have a lot of includes at the top of my page to include things like this menu ans then in the index.php i use their variables to put them in divs, the divs are later styled with css, or should i load everything in each separate php file if you know what l mean?
If you dont understand, do you have msn and could i add you?
Also, thank you very much for everything you have helped me with, i got iy to work for an instance and it does exactly what i want it to, just not sure how to load it...
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0
Yea, i was going to use includes, but should i echo everything in the php? or should l do it all in the index.php? It sort of hard to explain, do you have msn? Yes, you got the code i need, just am unsure on how to place it, due to my above question...
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0
okay... this will be a different approach for me, i will let you know how i go with it...
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0
ok, so im not real good at inserting html in php, but am i looking for something like this?
<?php
include 'mysqlconnect.php';
$db = mysql_select_db("edit_site", $con);
if (!$db)
{
die('Can not Connect to Database: ' . mysql_error());
}
$result=mysql_query('SELECT * FROM main_menu');
while($row=mysql_fetch_assoc($result)){
<html><body>
<div id="menu"
<ul id="main_menu">
echo "<li> <a link='".$row['link']."'>".$row['title']."</a></li>";
</ul>
</div>
</body></html>
}
?>
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0
ok, just needed to change the last line to this:
<?php } ?>
One more thing, not much of the css is applied, but i will work this out, how do i make it appear in a list formation? it just places 1 link next to another right now...
And do you have msn, if so could i add you, there are A LOT of problems with this code...
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0
There'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 id="menu">
Lsmjudoka
Junior Poster in Training
76 posts since Apr 2009
Reputation Points: 10
Solved Threads: 9
Skill Endorsements: 0
Not just is that easier Lsmjudoka but it actually applies all of my css that way, i could hug you right now : ] so would you know how to get my menu to appear in a list formation, instead of 1 button beside the other?
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0
<ul> will put <li> elements in a vertical format, unless you changed the CSS. Let me see your CSS... Or alternatively you can post it in the HTML/CSS forums where there will be someone better than me to answer that.
Lsmjudoka
Junior Poster in Training
76 posts since Apr 2009
Reputation Points: 10
Solved Threads: 9
Skill Endorsements: 0
<ul> will put <li> elements in a vertical format, unless you changed the CSS. Let me see your CSS... Or alternatively you can post it in the HTML/CSS forums where there will be someone better than me to answer that.
nah, its not the css, it worked fine before i added the php to display every m,enu item with only 1 line of code:
<li><a href="{$row['link']}">{$row['title']}</a></li>
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0
Any other ideas however, or are you still convinced its my css?
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0
Let me put it this way: What changed? Clearly something changed if it worked before, what is it that's different? Providing you have a <ul> tag before the loop and a </ul> tag after, the only potential cause is in your style sheets.
Lsmjudoka
Junior Poster in Training
76 posts since Apr 2009
Reputation Points: 10
Solved Threads: 9
Skill Endorsements: 0