2

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?

3
  • You just need two nested loops. Commented Sep 2, 2012 at 21:53
  • 1
    @PeterSzymkowski I respected the OP's wish to have ORDER BY sec_id ASC. If-you-know-what-I-mean. :) Commented Sep 2, 2012 at 22:09
  • 1
    @Nerd-Herd ok updated my answer. Still no need to use nested queries which makes groups qty*queries to db Commented Sep 2, 2012 at 22:12

2 Answers 2

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

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

// display data
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>";

There is another solution if you don't care about sorting result by sec_id

Sign up to request clarification or add additional context in comments.

Comments

0
$qry_secs="SELECT DISTINCT sec_group FROM tbl_user_sec ORDER BY sec_id ASC";
$result_secs = mysql_query($qry_secs);
echo '<ul>\n';
//echo values
while($row_secs = mysql_fetch_assoc($result_secs)) {
    echo '<li><a href="#">'.$row_secs['sec_group'].'</a></li>\n';
    echo '<ul>\n';
    $newqry = "SELECT sec_name FROM tbl_user_sec WHERE `sec_group` = '" . mysql_real_escape_string($row_secs['sec_group'] . "'";
    $result = mysql_query($newqry);
    while($row = mysql_fetch_assoc($result) ) {
        echo '<li><a href="#">' . $row['sec_name'] . '</li>\n';
    echo '</ul>\n';
}
echo '</ul>\n';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.