Recently i have made a Blog ie Programming tutorial blog
Can you please give me a logic for creating menu navigation like this:
Java >> Frameworks >> Struts2
What logic i have to use for this? I am very confused to think, is this logic happens with SQL query?
I am using : Codeigniter,php,My sql
My database structure is like this:
Table: menu
---------------
id PK(primary key)
menu_name .....
content longtext
parent_id int(key to id) Foreign key
This is an example of the data stored in the table:
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
One of the ideas is ...
CODE NOT TESTED but should get you going ....
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);
}
}
Add this function into your model class, Page_model.php
In your controlloer file include your model like;
$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>';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.