howard-moore Posted April 8, 2011 Share Posted April 8, 2011 Hi All, I have a snippet of code (below) which I use to create a dynamic menu from a MySQL database for a community website. Fields used in the database are: ID parent (what the parent page is) page_order (what is says on the tin) short_title (a short title for the page for the URL) title (the page title - used as the text for the link) content (the content for the page) I have got the dynamic list to display fine, but what I would like to be able to do is give each list item a 'class' tag when it has been selected (and thus is the page being shown). Can anyone suggest how to amend the below code to allow this? Code is: <!-- ##### START LIST_INFO_PAGES ##### --> <?php include("../config/config.php"); ?> <?php $sql = "SELECT * FROM `PCNET_$filename` WHERE parent = 'council' ORDER BY page_order ASC"; $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql); while ($row = mysql_fetch_assoc($sql_result)) { print ''; if ($row["status"]<>'') { echo '<li><a href="../council-indiv.php?id='; echo ($row["id"]).'&short_title='; echo nl2br(stripslashes(utf8_decode($row["short_title"]))).'" title='; echo nl2br(stripslashes(utf8_decode($row["title"]))).'>'; echo nl2br(stripslashes(utf8_decode($row["title"]))).'</a></li>'; }; }; ?> <!-- ##### END LIST_INFO_PAGES ##### --> Help will be welcome! Neil Quote Link to comment https://forums.phpfreaks.com/topic/233073-help-needed-php-dynamic-menu/ Share on other sites More sharing options...
howard-moore Posted April 8, 2011 Author Share Posted April 8, 2011 Hi All - an update! I have added a bit more code to create 'if' / 'else' depending on whether the particular menu list item (i.e. link) has been clicked. This then sets the menu list item to have a specific 'class' attributed to it, which is then coloured a specific colour by the CSS. However, the issue I have is that the particular element I have added (&focus=here), when added to the end of the URL, simply colours all the link button as 'here' buttons: <!-- ##### START LIST_INFO_PAGES ##### --> <?php include("../config/config.php"); ?> <?php $sql = "SELECT * FROM `PCNET_$filename` WHERE parent = 'council' AND type = 'Pages' ORDER BY page_order ASC"; $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql); while ($row = mysql_fetch_assoc($sql_result)) { print ''; if ($row["status"]<>'') { echo '<li><a href="../council-indiv.php?id='; echo ($row["id"]).'&name='; echo nl2br(stripslashes(utf8_decode($row["category"]))).'&focus=here" title="'; echo nl2br(stripslashes(utf8_decode($row["title"]))).'"'; if ($focus = here) { echo ' class="subthere">'; } else { echo '>'; } echo nl2br(stripslashes(utf8_decode($row["category"]))).'</a></li>'; }; }; ?> <!-- ##### END LIST_INFO_PAGES ##### --> Any help would be gratefully received. Neil Quote Link to comment https://forums.phpfreaks.com/topic/233073-help-needed-php-dynamic-menu/#findComment-1198661 Share on other sites More sharing options...
howard-moore Posted April 8, 2011 Author Share Posted April 8, 2011 Really? No-one can help with this one? Quote Link to comment https://forums.phpfreaks.com/topic/233073-help-needed-php-dynamic-menu/#findComment-1198782 Share on other sites More sharing options...
dcro2 Posted April 8, 2011 Share Posted April 8, 2011 Tryyy if ($focus == "here") { Notice the two equal signs . Btw, if your doctype is xhtml you should be using & instead of & in a link. Quote Link to comment https://forums.phpfreaks.com/topic/233073-help-needed-php-dynamic-menu/#findComment-1198787 Share on other sites More sharing options...
howard-moore Posted April 8, 2011 Author Share Posted April 8, 2011 Hmm... didn't seem to work. Perhaps if I give a bit more detail about how the page set-up works: I have a page called council-indiv.php which is the template for the content stored in the MySQL database. The problem with the code that I have found is that if I add a particular string to the URL (such as 'focus=here') it returns ALL the list items that make up the dynamic menu as containing focus=here. Therefore, they all show the css style for a menu item that is selected. Really scratching my head here! Quote Link to comment https://forums.phpfreaks.com/topic/233073-help-needed-php-dynamic-menu/#findComment-1198801 Share on other sites More sharing options...
dcro2 Posted April 8, 2011 Share Posted April 8, 2011 What exactly is setting '&focus=here' supposed to do? Quote Link to comment https://forums.phpfreaks.com/topic/233073-help-needed-php-dynamic-menu/#findComment-1198823 Share on other sites More sharing options...
howard-moore Posted April 8, 2011 Author Share Posted April 8, 2011 That's kind of where I am struggling. I thought that if I added it to the URL it would mean that when it was clicked (thus calling the data through the council-indiv.php page) it would show that button with the different css class (class="subthere"). However, I can see that I am wrong, as it simply colours all buttons with that class. Any other ideas how to get a menu 'selected' button to work? Quote Link to comment https://forums.phpfreaks.com/topic/233073-help-needed-php-dynamic-menu/#findComment-1198832 Share on other sites More sharing options...
dcro2 Posted April 8, 2011 Share Posted April 8, 2011 Right. Well, since you're passing the id in the link anyways, why not just use it to match the row you're echoing? if($_GET['id'] == $row['id']) { echo ' class="subthere"'; } echo '>'; Quote Link to comment https://forums.phpfreaks.com/topic/233073-help-needed-php-dynamic-menu/#findComment-1198836 Share on other sites More sharing options...
howard-moore Posted April 8, 2011 Author Share Posted April 8, 2011 dcro2 - you are a complete legend. That is what I was trying to do all along! Thank you so, so much! Quote Link to comment https://forums.phpfreaks.com/topic/233073-help-needed-php-dynamic-menu/#findComment-1198840 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.