Skip to content

davicotico/PHP-Quick-Menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Quick Menu

Esta clase PHP permite crear un menu Html a partir de una string Json. Por favor califica este repositorio, pues de esa forma voy a saber que este trabajo está siendo útil. La clase tiene parámetros de configuración, explicados en este tutorial: http://codeignitertutoriales.com/php-menu-dinamico-multinivel/

Yes

Input

[{
	"text": "Home",
	"href": "#home",
	"title": "Home"
}, {
	"text": "About",
	"href": "#",
	"title": "About",
	"children": [{
		"text": "Action",
		"href": "#action",
		"title": "Action"
	}, {
		"text": "Another action",
		"href": "#another",
		"title": "Another action"
	}]
}, {
	"text": "Something else here",
	"href": "#something",
	"title": "Something else here"
}]

Output

<ul class="nav navbar-nav" id="#myMenu">
   <li><a href="#home" title="Home">Home</a></li>
   <li class="dropdown">
      <a href="#" title="About" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">About <i class="caret"></i></a>
      <ul class="dropdown-menu">
         <li><a href="#action" title="Action">Action</a></li>
         <li><a href="#another" title="Another action">Another action</a></li>
      </ul>
   </li>
   <li><a href="#something" title="Something else here">Something else here</a></li>
</ul>

How to use

  • Extend the QuickMenu class for customization. For instance Bootstrap menu. (This is optional)
require 'QuickMenu.php';
class BootstrapMenu extends QuickMenu
{
    public function __construct($options = array())
    {
        parent::__construct($options);
        $this->setDropDownIcon('<i class="caret"></i>');
        $this->set('ul-root', array('class'=>'nav navbar-nav', 'id'=>'#myMenu'));
        $this->set('ul', array('class'=>'dropdown-menu'));
        $this->set('li-parent', array('class'=>'dropdown'));
        $this->set('a-parent', array('class'=>"dropdown-toggle", 'data-toggle'=>"dropdown", 'role'=>"button", 'aria-haspopup'=>"true", 'aria-expanded'=>"false"));
    }
}
  • Include your class
include "BootstrapMenu.php";
$str = '[{"text":"Home", "href": "#home", "title": "Home"}, {"text":"About", "href": "#", "title": "About", "children": [{"text":"Action", "href": "#action", "title": "Action"}, {"text":"Another action", "href": "#another", "title": "Another action"}]}, {"text":"Something else here", "href": "#something", "title": "Something else here"}]';
  • Instance the class with data parameters
$qMenu = new BootstrapMenu(array('data'=>$str));
  • Use the methods availables
$qMenu->setActiveItem('http://codeignitertutoriales.com');
$qMenu->insert(array("text"=>'Ooh!', "href"=>'http://codeignitertutoriales.com', "title"=>'Awesome'), 'Another action', 'About');
$qMenu->insert(array("text"=>'Ultimo item', "href"=>'https://github.com/davicotico', "title"=>'My Github'));
  • Renderize the menu in a string variable
$menu = $qMenu->html();
  • Done. You can echoes the menu on html document
<div id="navbar" class="navbar-collapse collapse">
    <?php echo $menu ?>
</div>

Class reference

setActiveItem($href, $activeClass = '')

Setting the active item.

Parameters:

  • string $href The active href
  • string $activeClass (Optional) The Css class for the active item

insert($item, $before_at = '', $parent = '')

Insert an item

Parameters:

  • array $item - Associative array with item attributes (text, href, icon, title)
  • string $before_at (Optional) The reference position for insert
  • string $parent (Optional) The parent if the insert is in submenu

remove($text)

Remove an item (from top level) by text attribute

Parameters:

  • string $text Text item

replace(array $newItem, $text)

Replace an item (find by text attribute)

Parameters:

  • array $newItem The new item
  • string $text The text item for search

html()

The Html menu

return: string Html menu

setData($data)

Setting the data from json string or associative array

Parameters:

  • mixed $data Data (Json string or associative array)

setResult($result, $columnID, $columnParent)

Set result from query database

Parameters:

  • array $result The resultset
  • string $columnID The ID column name (Primary key)
  • string $columnParent The column name for identify the parent item

setDropdownIcon($content)

Set dropdown icon for display with submenus

Parameters:

  • string $content Content for the dropdown icon (Html code)

set($name, $value)

Set the attributes for the tag vars

Parameters:

  • string $name Var name
  • mixed $value Var value

Var names: 'ul', 'ul-root', 'li', 'li-parent', 'a', 'a-parent', 'active-class'