Jump to content

[SOLVED] dynamic menu


pietbez

Recommended Posts

i know this might sound like a stupid question to all you pros, but to me this is a mountain ???

 

what would be the sensible way to code this?

 

<?php

include('qazwsxedc/config.php');

mysql_connect($server,$username,$password);
@mysql_select_db($database) or die ("Unable to connect to the database");


$button1 = mysql_query("SELECT title FROM menu where id=1");
$but1 = mysql_fetch_array( $button1 );

$button2 = mysql_query("SELECT title FROM menu where id=2");
$but2 = mysql_fetch_array( $button2 );

$button3 = mysql_query("SELECT title FROM menu where id=3");
$but3 = mysql_fetch_array( $button3 );

$button4 = mysql_query("SELECT title FROM menu where id=4");
$but4 = mysql_fetch_array( $button4 );


?>           
<li><a href="#"><span><?php echo $but1['title'];?></span></a></li>
<li><a href="#"><span><?php echo $but2['title'];?></span></a></li>
<li><a href="#"><span><?php echo $but3['title'];?></span></a></li>
<li><a href="#"><span><?php echo $but4['title'];?></span></a></li>

Link to comment
https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/
Share on other sites

Are there more than 4 id's that you want or do you want all of them?

 

If you want all of them and pull them out one by one you can do this:

$query=mysql_query("SELECT title FROM menu ORDER BY id ASC");
while ($row=mysql_fetch_assoc($query)) {
  echo '<li><a href="#"><span>'.$row['title'].'</span></a></li>'."\n";
}

Link to comment
https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/#findComment-800452
Share on other sites

$result = mysql_query("SELECT title FROM menu where id IN(1, 2, 3, 4) ORDER BY id");
while ($row = mysql_fetch_assoc($result)) {
    echo '<li><a href="#"><span>' . $row['title'] . '</span></a></li>';
}

 

Just a bit variation of Yesideez, but yea. This does not do all just the ID's you want (depends on what you want for which to use.)

 

Link to comment
https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/#findComment-800455
Share on other sites

You can go one step further by including the link itself inside the database:

$query=mysql_query("SELECT title,link FROM menu ORDER BY id ASC");
while ($row=mysql_fetch_assoc($query)) {
  echo '<li><a href="'.$row['link'].'"><span>'.$row['title'].'</span></a></li>'."\n";
}

 

Any reason why you've got an empty <span> in there? It doesn't appear to be doing anything!

Link to comment
https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/#findComment-800458
Share on other sites

oops sorry,

 

<li id="current"><a href="#"><span><?php echo $but1['title'];?></span></a></li>         

<li><a href="#"><span><?php echo $but2['title'];?></span></a></li>

<li><a href="#"><span><?php echo $but3['title'];?></span></a></li>

<li><a href="#"><span><?php echo $but4['title'];?></span></a></li>

 

http://76.162.122.173/mygreenclean/index.php

 

here you can see what im atempting to do

Link to comment
https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/#findComment-800477
Share on other sites

no, thanks, its working fine, it was just in responce to your question about <span>

 

the first link in the menu is active by default

 

<li id="current"><a href="#"><span><?php echo $but1['title'];?></span></a></li>         

<li><a href="#"><span><?php echo $but2['title'];?></span></a></li>

<li><a href="#"><span><?php echo $but3['title'];?></span></a></li>

<li><a href="#"><span><?php echo $but4['title'];?></span></a></li>

 

does that have anything to do with <span>?

Link to comment
https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/#findComment-800486
Share on other sites

Oh yes, I see now - <span> is needed by the CSS :D

 

If you want the first link to be populated try this:

$query=mysql_query("SELECT id,title FROM menu ORDER BY id ASC");
while ($row=mysql_fetch_assoc($query)) {
  echo '<li'.($row['id']==1 ? ' id="current"' : '').'><a href="#"><span>'.$row['title'].'</span></a></li>'."\n";
}

 

EDIT: Added "id" into query

Link to comment
https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/#findComment-800489
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.