0

最近我做了一个博客即编程教程博客

你能给我一个创建这样的菜单导航的逻辑吗:

Java >> Frameworks >> Struts2

我必须为此使用什么逻辑?我很困惑地想,这种逻辑是否发生在 SQL 查询中?


我正在使用:Codeigniter,php,我的 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
4

1 回答 1

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>';
于 2013-06-19T05:32:30.027 回答