Jump to content

Dynamic PHP/mySQL Menu & Sub Menu Links ..??


azukah

Recommended Posts

Hi- I want to have a dynamic menu that feeds from a table using php and mysql.

 

My table looks like this:

sec_id

sec_name

sec_group

1

section 1

group 1

2

section 2

group 1

3

section 3

group 2

4

section 4

group 1

5

section 5

group 3

 

I can do a query to get and display unique sec_group values but can't figure out a way to include sec_name into each sec_group

//Query by unique sec_group
$qry_secs="SELECT DISTINCT sec_group FROM tbl_user_sec ORDER BY sec_id ASC";
$result_secs = mysql_query($qry_secs);

//echo values
while($row_secs = mysql_fetch_assoc($result_secs)){
echo '<ul><li><a href="#">'.$row_secs['sec_group'].'</a></li></ul>';
}

 

Eventually, the HTML should like the code below.

<ul>
<li><a href="#">Group 1</a>
      <ul>
      <li><a href="#">Section 1</a></li>
      <li><a href="#">Section 2</a></li>
      <li><a href="#">Section 4</a></li>
      </ul>
</li>

<li><a href="#">Group 2</a>
      <ul>
      <li><a href="#">Section 3</a></li>
      </ul>
</li>

<li><a href="#">Group 3</a>
      <ul>
      <li><a href="#">Section 5</a></li>
      </ul>
</li>
</ul>

 

Any ideas???

Link to comment
https://forums.phpfreaks.com/topic/267924-dynamic-phpmysql-menu-sub-menu-links/
Share on other sites

got a reply in another forum...

 

$q = mysql_query("SELECT sec_id, sec_name, sec_group FROM tbl_user_sec ORDER BY sec_id");

$groups = Array();
while($w = mysql_fetch_assoc($q)) {
  if(!isset($groups[$w['sec_group']])) $groups[$w['sec_group']] = Array();
  $groups[$w['sec_group']][] = $w;
}

echo "<ul>";
foreach($groups as $group_name => $sections) {
  echo '<li><a href="#">'.$group_name.'</a><ul>';
  foreach($sections as $section) {
    echo '<li><a href="#">'.$section['sec_name'].'</a>';
  }
  echo '</ul></li>';
}
echo "</ul>";

 

http://stackoverflow.com/questions/12239995/how-to-create-dynamic-menu-with-sub-menu-with-php-mysql

 

This is how I'd do it, as it cuts down on the number of loops and keeps everything nice and tidy. ;)

$q = mysql_query ("SELECT sec_id, sec_name, sec_group FROM tbl_user_sec ORDER BY sec_id");

$output = $prevGroup = '';

while ($row = mysql_fetch_assoc ($q)) {
// Check if a new group has been found
if ($row['sec_group'] != $prevGroup) {
	// If not the first group in the set, close the previous.
	if (!empty ($prevGroup)) {
		$output .= "\t</ul></li>\n";
	}

	// Add grouping header to output, and save group name for above check.
	$output .= "\t" . '<li><a href="#">' . $row['sec_group'] . "</a><ul>\n";
	$prevGroup = $row['sec_group'];
}

// Add section name to menu output.
$output .= "\t\t" . '<li><a href="#">' . $row['sec_name'] . "</a></li>\n";
}

// Finish list markup.
$output = '<ul>' . $output . "\t</ul></li>\n</ul>\n";

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.