Welcome Guest, Not a member yet? Register   Sign In
Help Create sub menu from database!
#5

[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


Messages In This Thread
Help Create sub menu from database! - by El Forum - 08-12-2012, 08:46 AM
Help Create sub menu from database! - by El Forum - 08-13-2012, 03:49 AM
Help Create sub menu from database! - by El Forum - 08-13-2012, 04:28 AM
Help Create sub menu from database! - by El Forum - 08-13-2012, 06:54 AM
Help Create sub menu from database! - by El Forum - 08-13-2012, 07:41 AM
Help Create sub menu from database! - by El Forum - 08-13-2012, 07:56 AM
Help Create sub menu from database! - by El Forum - 08-13-2012, 09:56 AM
Help Create sub menu from database! - by El Forum - 08-13-2012, 10:01 AM
Help Create sub menu from database! - by El Forum - 08-13-2012, 10:19 AM
Help Create sub menu from database! - by El Forum - 08-13-2012, 10:25 AM
Help Create sub menu from database! - by El Forum - 08-13-2012, 11:01 AM
Help Create sub menu from database! - by El Forum - 08-13-2012, 11:48 AM
Help Create sub menu from database! - by El Forum - 08-13-2012, 11:59 AM
Help Create sub menu from database! - by El Forum - 08-13-2012, 12:10 PM
Help Create sub menu from database! - by El Forum - 08-13-2012, 06:29 PM
Help Create sub menu from database! - by El Forum - 08-13-2012, 06:37 PM
Help Create sub menu from database! - by El Forum - 08-13-2012, 10:20 PM
Help Create sub menu from database! - by El Forum - 08-16-2012, 04:48 PM



Theme © iAndrew 2016 - Forum software by © MyBB