CodeIgniter Forums
Help Create sub menu from database! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Help Create sub menu from database! (/showthread.php?tid=53863)

Pages: 1 2


Help Create sub menu from database! - El Forum - 08-12-2012

[eluser]uqmeebi[/eluser]
hello all gurus!
i need create sub menu from database but i dont know how?
please help me.

example:

1 menu item | 2 menu item | 3 menu item| 4 menu item
1.1 sub item
1.2 sub item
1.3 sub item
1.4 sub item


Help Create sub menu from database! - El Forum - 08-13-2012

[eluser]Egill Th[/eluser]
I'm not sure what you're asking for but to my understanding you are looking for something like this.

There may be some errors but it should work in theory.

controller
Code:
<?php
class Item extends CI_Controller {
  var $data;

  public function __construct() {
    $this->load->model('itemmodel');

  public function showMenu() {
    $this->data['items'] = $this->itemmodel->getItems();
    
    foreach ( $this->data['items'] as $item ) {
      $this->data['sub_items'][$item->id] = $this->itemmodel->getSubItems($item->id);
    }

    $this->load->view('itemview.php', $this->data);
}

view
Code:
<?php
foreach ( $items as $item ) {
  echo $item->name;

  foreach ( $sub_items[$item->id] as $sub_item ) {
    echo $sub_item->name;
  }
}



Help Create sub menu from database! - El Forum - 08-13-2012

[eluser]Krystian[/eluser]
you need to create table like this

id
label
parent_id


so the entries for you example would be

1 | menu_item_1 | 0 |
2 | sub 1 menu_item_1 | 1 |
3 | sub 2 menu_item_1 | 1 |
4 | sub 3 menu_item_1 | 1 |
5 | sub 4 menu_item_1 | 1 |
6 | menu_item_2 | 0 |
7 | menu_item_3 | 0 |
8 | menu_item_4 | 0 |

and you need to proper print it and style it





Help Create sub menu from database! - El Forum - 08-13-2012

[eluser]uqmeebi[/eluser]
[quote author="Krystian" date="1344857300"]you need to create table like this

id
label
parent_id


so the entries for you example would be

1 | menu_item_1 | 0 |
2 | sub 1 menu_item_1 | 1 |
3 | sub 2 menu_item_1 | 1 |
4 | sub 3 menu_item_1 | 1 |
5 | sub 4 menu_item_1 | 1 |
6 | menu_item_2 | 0 |
7 | menu_item_3 | 0 |
8 | menu_item_4 | 0 |

and you need to proper print it and style it


[/quote]


thx but i don't understand how create MODEL for query please create simple file for example.


Help Create sub menu from database! - El Forum - 08-13-2012

[eluser]LonelyWolf[/eluser]
Hi all, excuse my little english AND please don't hurt, i found codeigniter only yesterday evening.

This is what i'm doing and at this stage there's nothing to insert voices in the menĂ¹ use phpmyadmin or any other client to your database.

I've created a quick library to accomplish this, still required a db table.
The table:
Code:
CREATE TABLE IF NOT EXISTS `navigation` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'General index/key',
  `father` int(10) unsigned NOT NULL COMMENT 'Item\'s Father',
  `menuorder` smallint(5) unsigned NOT NULL COMMENT 'Item\'s display order',
  `item` varchar(20) NOT NULL COMMENT 'Item displayed',
  `destination` varchar(200) NOT NULL COMMENT 'Destination Link',
  PRIMARY KEY (`id`),
  KEY `codice_menu` (`padre`,`ordine`)
) ENGINE=InnoDB ;

The main menĂ¹ supposed to be with father=0, sub menu items will have the relative item id as father.

In application/libraries/Navigationmenu.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* CodeIgniter navigationMenu Class
*
* This class enables the creation of menus
*
*/
class CI_Navigationmenu {

var $CI;
/**
  * Constructor
  */
public function __construct()
{
  $this->CI =& get_instance();
  log_message('debug', "Navigationmenu Class Initialized");
    $this->CI->load->helper('url');
}
/**
  * Generate the menu
  *
  * @access public
  * @param integer the menu to display
  * @return string
  */
function generate($padre)
{

    $out = '<ul class="menu">';
    $query = $this->CI->db->query("select * from navigation where padre = $padre order by ordine;");
    foreach($query->result_array() as $menu_item) {
      $out .= '<li class="menu"><a class ="menu" href="'.base_url().$menu_item['destinazione'].'">'.$menu_item['voce'].'</a></li>';
    }
    $out .= '</ul>';
  return $out;
}



}

// END CI_NavigationMenu class

/* End of file Navigationmenu.php */
/* Location: ./application/libraries/Navigationmenu.php */
This code uses some style to display horizontally list items, you can style as you like.

Load in your controller:
Code:
$this->load->library('navigationmenu');

Show in your pages:
Code:
echo $this->navigationmenu->generate(0);
Change generate parameter to display different submenu.

ToDo:
* Display fathers menu item when showing submenu
* Anything to manage menu items
* clear the code

Edit:
Corrected link generation url


Help Create sub menu from database! - El Forum - 08-13-2012

[eluser]InsiteFX[/eluser]
PHP Dynamic Menu - Just save to database instead



Help Create sub menu from database! - El Forum - 08-13-2012

[eluser]uqmeebi[/eluser]
[quote author="InsiteFX" date="1344869787"]PHP Dynamic Menu - Just save to database instead
[/quote]

sorry but it's not solution. we need MVC structure for codeigniter.


Help Create sub menu from database! - El Forum - 08-13-2012

[eluser]uqmeebi[/eluser]
[quote author="Egill Th" date="1344854984"]I'm not sure what you're asking for but to my understanding you are looking for something like this.

There may be some errors but it should work in theory.

controller
Code:
&lt;?php
class Item extends CI_Controller {
  var $data;

  public function __construct() {
    $this->load->model('itemmodel');

  public function showMenu() {
    $this->data['items'] = $this->itemmodel->getItems();
    
    foreach ( $this->data['items'] as $item ) {
      $this->data['sub_items'][$item->id] = $this->itemmodel->getSubItems($item->id);
    }

    $this->load->view('itemview.php', $this->data);
}

view
Code:
&lt;?php
foreach ( $items as $item ) {
  echo $item->name;

  foreach ( $sub_items[$item->id] as $sub_item ) {
    echo $sub_item->name;
  }
}
[/quote]

please create full MVC script for visualization.


Help Create sub menu from database! - El Forum - 08-13-2012

[eluser]TWP Marketing[/eluser]
[quote author="uqmeebi" date="1344877271"]

please create full MVC script for visualization.[/quote]

Ok, then you want to post your request in the jobs forum and ask someone to design this for you, for pay.


Help Create sub menu from database! - El Forum - 08-13-2012

[eluser]uqmeebi[/eluser]
[quote author="TWP Marketing" date="1344878393"][quote author="uqmeebi" date="1344877271"]

please create full MVC script for visualization.[/quote]

Ok, then you want to post your request in the jobs forum and ask someone to design this for you, for pay.[/quote]

if i need this i make this. but i don't. and up script don't work.