最近我发了一个博客,即编程教程博客
请你给我一个创建菜单导航的逻辑:
Java >> Frameworks >> Struts2
我必须使用什么逻辑?我很困惑地想,这个逻辑是否发生在SQL查询中?
我正在使用:Codeigniter,php,My sql
我的数据库结构如下:
Table: menu
---------------
id PK(primary key)
menu_name .....
content longtext
parent_id int(key to id) Foreign key
这是存储在表格中的数据示例:
Example
----------------
id | menu_name | content | parent_id
----------------------------------------
1 | main 1 | this is main menu 1 | 0 <-- First level menu
2 | main 2 | this is main menu 2 | 0 <-- First level menu
3 | submenu 1 | this is main menu 1's first submenu's item 1 | 1 <-- Second level menu
4 | submenu 1 | this is main menu 1's first submenu's item 2 | 1 <-- Second level menu
5 | submenu 2 | this is main menu 2's first submenu's item 1 | 2 <-- Second level menu
6 | submenu 1-1 | this is submenu 1's first submenu's item 1 | 3 <-- Third level menu
7 | submenu 1-2 | this is submenu 1's first submenu's item 2 | 3 <-- Third level menu
答案 0 :(得分:0)
其中一个想法是......
代码没有被测试但是应该让你去....
public function get_crumbs($id, $crumbs=array()) {
$query = $this->db->query("select menu_name, parent_id from mytable where id=?", array($id));
$row = $query->row_array();
$crumbs[] = $row['menu_name'];
if($row['parent_id'] == 0) {
krsort($crumbs); //sort array in descending order
$crumbs_str = implode(' >> ', $crumbs);
return $crumbs_str;
} else {
$this->get_crumbs($row['parent_id'], $crumbs);
}
}
将此函数添加到模型类Page_model.php
中在您的控制器文件中包含您的模型;
$this->load->model('Page_model', 'page_model');
//Get ID
$id = $this->uri->segment(4);
$data['crumbs'] = $this->page_model->get_crumbs($id);
//Load your view
$this->load->view('content', $data);
//In your view file
echo '<p><strong>'.$crumbs.'</strong></p>';