1,105,332 Community Members

Laoding Menu Links from a DB with PHP

Member Avatar
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
P0lT10n
Posting Whiz in Training
237 posts since Apr 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 30 [?]
Skill Endorsements: 0 [?]
 
2
 

You 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 something of menu, you must enter to the table and do stuff... with this you update all the pages...

Member Avatar
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
P0lT10n
Posting Whiz in Training
237 posts since Apr 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 30 [?]
Skill Endorsements: 0 [?]
 
1
 

Suppose 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>";
} 
?>

I used a simple paragraph to show menu where with <a> tag you give the paragraph an URL to go when you press the $row that will show the menu name (ex. Home, about, firum)... And with while you will echo the code all the time that $row can get the return value of mysql_fetch_assoc...

Member Avatar
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
P0lT10n
Posting Whiz in Training
237 posts since Apr 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 30 [?]
Skill Endorsements: 0 [?]
 
0
 

do this

echo "<a href='".$row['href']."'>".$row['name']."</a>";
Member Avatar
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
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...

Member Avatar
P0lT10n
Posting Whiz in Training
237 posts since Apr 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 30 [?]
Skill Endorsements: 0 [?]
 
1
 

Do 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 !

Member Avatar
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
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...

Member Avatar
P0lT10n
Posting Whiz in Training
237 posts since Apr 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 30 [?]
Skill Endorsements: 0 [?]
 
0
 

Echo all in their files, not in index.php

Member Avatar
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

okay... this will be a different approach for me, i will let you know how i go with it...

Member Avatar
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
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>
}
?>
Member Avatar
P0lT10n
Posting Whiz in Training
237 posts since Apr 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 30 [?]
Skill Endorsements: 0 [?]
 
0
 

You did something wrong

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>
}

When you want to show html code, always close php code, like this

while($row=mysql_fetch_assoc($result)){ ?>
<html><body>
<div id="menu"
<ul id="main_menu">
<li><a href="<?php echo $row['link']; ?>">"<?php echo $row['title']; ?>"</a></li>
</ul>
</div>
</body></html>
<? }

and then re open php code if necesary for close something like while...
And it's href, not link when you use <a> tags

Member Avatar
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
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...

Member Avatar
Lsmjudoka
Junior Poster
139 posts since Apr 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
 
1
 

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">

Member Avatar
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
Lsmjudoka
Junior Poster
139 posts since Apr 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
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>
Member Avatar
dlannetts
Junior Poster in Training
85 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Any other ideas however, or are you still convinced its my css?

Member Avatar
Lsmjudoka
Junior Poster
139 posts since Apr 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
 
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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: