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 for P0lT10n

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

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 for P0lT10n

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

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 for P0lT10n

do this

echo "<a href='".$row['href']."'>".$row['name']."</a>";

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 for P0lT10n

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 !

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 for P0lT10n

Echo all in their files, not in index.php

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

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 for P0lT10n

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

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

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

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?

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

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

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

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.

Sorry about long response, well, before i added any php to the menu at all, the menu was in a vertical form, now however after the php it is in horizontal form, and i haven't changed any css as of yet, the is the php

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;
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.