php - Dynamically building horizontal menu -


i have javascript code allows me create horizontal menu sub menus so:

<ul id="menu"> <li>menu 1   <ul>    <li>sub menu 1</li>   </ul> </li> </ul> 

i can create many sub menus want, problem i'm using php grab links mysql database , don't know how can dynamically build these sub menus without manually checking sub menu on , on again. example in mysql table:

fields: menu_id menu_name menu_link menu_parentid

so menu id auto increment , menu_parentid allows me assign sub menu name/link parent menu. in order 2 sub menu checks:

$query = "select * site_menu menu_parentid = 0";  foreach($query $q) { //run through results $query2 = "select * site_menu menu_parentid = $q['id']";  foreach($query2 $q2) { //run through results } } 

as can see have query twice first sub menu, if there third sub menu? have run 3 queries? suggestions?

perhaps function or do..while loop may in order? proof of concept:

function menuquery($id) {     $query = "select * site_menu menu_parentid = $id";      if ($query) {         foreach($query $q) {             //run through results             menuquery($q->id);         }     } }  //initial call of top level menu items menuquery(0); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -