pietbez Posted April 3, 2009 Share Posted April 3, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/ Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/#findComment-800452 Share on other sites More sharing options...
premiso Posted April 3, 2009 Share Posted April 3, 2009 $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.) Quote Link to comment https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/#findComment-800455 Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 ps - nothing is stupid if you don't know the answer! Quote Link to comment https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/#findComment-800456 Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/#findComment-800458 Share on other sites More sharing options...
pietbez Posted April 3, 2009 Author Share Posted April 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/#findComment-800477 Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 And? I can't see anything different to what we've shown you... ...is there something we've missed? Quote Link to comment https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/#findComment-800480 Share on other sites More sharing options...
pietbez Posted April 3, 2009 Author Share Posted April 3, 2009 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>? Quote Link to comment https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/#findComment-800486 Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 Oh yes, I see now - <span> is needed by the CSS 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 Quote Link to comment https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/#findComment-800489 Share on other sites More sharing options...
pietbez Posted April 3, 2009 Author Share Posted April 3, 2009 thanks, its perfect, slowly i learn Quote Link to comment https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/#findComment-800501 Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 You're welcome! I added what's called a ternary conditional condition ? true : false http://www.addedbytes.com/php/ternary-conditionals/ Can save a lot of code by crunching it down to one line (as with your echo above!) Quote Link to comment https://forums.phpfreaks.com/topic/152417-solved-dynamic-menu/#findComment-800503 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.