There are some ideas for creating a dropdown menu from data populated from xml or json. But how can I do this purely with php? A possible method is to create a multi-level array. But what is the best way to create it from mysql data (having parent id for sub-menus), and how to effectively read the array (via a foreach loop?)?
2条回答 默认 最新
- douhuanqiao5290 2011-10-01 06:53关注
for php side, lets assume you retrieve using that query what is in the table in an array of format:
$menu = array( 'page/1' => 'about', 'page/2' => 'photos', 'menu-title' => array('page/4' => 'sub-menu-1','page/5' => 'sub-menu-2') ); echo '<ul>'; foreach($menu as $key => $value){ if(is_string($value)){ echo '<li><a href="'.$key.'">'.$value.'</a></li>'; } if(is_array($value)){ echo '<ul>'; echo '<li><a href="#">'.key($value).'</a></li>'; foreach($value as $sub_key => $sub_value){ echo '<li><a href="'.$sub_key .'">'.$sub_value.'</a></li>'; } echo '</ul>'; } } echo '</ul>';
for mysql database, have a column of "parent_id" if it is null then it would be a root level, other than that, it would be a sub there, another column of "path" and "title,you can add a "weight" column as well to order them.
NOTE: this is untested code
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 关于#hadoop#的问题
- ¥15 (标签-Python|关键词-socket)
- ¥15 keil里为什么main.c定义的函数在it.c调用不了
- ¥50 切换TabTip键盘的输入法
- ¥15 可否在不同线程中调用封装数据库操作的类
- ¥15 微带串馈天线阵列每个阵元宽度计算
- ¥15 keil的map文件中Image component sizes各项意思
- ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
- ¥15 划分vlan后,链路不通了?
- ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据