php - Materialize dropdown issue -
i'm trying make dynamic dropdown menu. have database table dropdown menus.
position means position in menu. dropdown (0 - no dropdown, 1 - dropdown "header" arrow, 2 - dropdown item). dropdown-parent means id of dropdown header.
problem can't figure out how implement it.
$query = $db->query("select * `menu` `dropdown` = 0 or `dropdown` = 1 order `position` asc"); foreach($query $row) { if($row['dropdown'] == 0) { echo '<li><a href="'.$row['url'].'">'.$row['name'].'</a></li>'; } elseif($row['dropdown'] == 1) { echo '<li><a class="dropdown-button" href="#" data-activates="dropdown'.$row['position'].'" data-beloworigin="true" data-hover="true">'.$row['name'].'<i class="material-icons right">arrow_drop_down</i></a></li>'; } }
this code works, dropdown items need edit
<ul id="dropdown2" class="dropdown-content"> <li><a href="#">test #1</a></li> </ul>
do have idea how implement it?
edit
this want
<ul id="dropdown1" class="dropdown-content"> <li><a href="#">test item #1 dropdown1</a></li> <li><a href="#">test item #2 dropdown1</a></li> <li><a href="#">test item #x dropdown1</a></li> </ul> <ul id="dropdown2" class="dropdown-content"> <li><a href="#">test item #1 dropdown2</a></li> <li><a href="#">test item #2 dropdown2</a></li> </ul> <ul id="dropdownx" class="dropdown-content"> <li><a href="#">test item dropdownx</a></li> </ul> <ul class="right hide-on-med-and-down"> <li><a href="#">header without dropdown</a></li> <li><a class="dropdown-button" href="#" data-activates="dropdown1">header dropdown1 items</a></li> <li><a class="dropdown-button" href="#" data-activates="dropdown2">header dropdown2 items</a></li> <li><a class="dropdown-button" href="#" data-activates="dropdownx">header dropdownx items</a></li> </ul>
i tried this
$query = $db->query("select * `menu` `dropdown` = 2 order position asc"); foreach($query $row) { echo '<ul id="dropdown'.$row['dropdown-parent'].'" class="dropdown-content"> <li><a href="'.$row['url'].'">'.$row['name'].'</a></li> </ul>'; }
but i'll get
<ul id="dropdown1" class="dropdown-content"> <li><a href="#">test item #1 dropdown1</a></li> </ul> <ul id="dropdown1" class="dropdown-content"> <li><a href="#">test item #2 dropdown1</a></li> </ul> <ul id="dropdown1" class="dropdown-content"> <li><a href="#">test item #x dropdown1</a></li> </ul> <ul id="dropdown2" class="dropdown-content"> <li><a href="#">test item #1 dropdown2</a></li> </ul> <ul id="dropdown2" class="dropdown-content"> <li><a href="#">test item #2 dropdown2</a></li> </ul> <ul id="dropdownx" class="dropdown-content"> <li><a href="#">test item #1 dropdownx</a></li> </ul>
there's problem in foreach, don't know hot fix it.
you need make <ul>
based on logic. there 3 steps. on first one, need end 1 , create another, on last one. below untested attempt. should close. note changed $query
$rows
clarity.
$rows = $db->query("select * `menu` `dropdown` = 2 order position asc"); $start = true; // know starting foreach($rows $row) { if($start){ // need create open ul tag echo '<ul id="dropdown'.$row['dropdown-parent'].'" class="dropdown-content">'; $start = false; // never again. } if(isset($lastrowparent) && $lastrowparent != $row['dropdown-parent']){ echo '</ul>'; //end last dropdown // start next 1 echo '<ul id="dropdown'.$row['dropdown-parent'].'" class="dropdown-content">'; } echo '<li><a href="'.$row['url'].'">'.$row['name'].'</a></li>'; //output content $lastrowparent = $row['dropdown-parent']; // update parent } echo '</ul>'; //when loop ends last ul not closed. close it.
Comments
Post a Comment