Jump to content

Help needed - php dynamic menu


howard-moore

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/233073-help-needed-php-dynamic-menu/
Share on other sites

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

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!

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?

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.