CodeIgniter - Form: Creating Menu Library

CodeIgniter Form Step By Step Tutorial - Part 4: After create simple layout, at this step, we will learn about library. For practice, we put our menu to library.

First, create a file named "MyMenu.php" within CodeIgniter\system\application\libraries. Enter following code to that file:

class MyMenu{
 	function show_menu(){
  		$obj =& get_instance();
  		$menu  = "<ul>";
  		$menu .= "<li>";
  		$menu .= anchor("books/main","List of Books");
  		$menu .= "</li>";
  		$menu .= "<li>";		
  		$menu .= anchor("books/input","Input Book");		
  		$menu .= "</li>";		
  		$menu .= "</ul>";
  		return $menu;

At creating of menu library, we need a class (we give name "MyMenu"). It have a function show_menu(). This function access other CI classes and helpers (URL helper). URL helper will help you make url easier:

 anchor("books/input","Input Book") 
 <a href="http://localhost/CodeIgniter/index.php/
books/input">Input Book</a>

This menu, we use <ul> and <li>. We will modify layout of menu use css (at next topic about css).

Now, open our controller: books.php within CodeIgniter\system\application\controllers. Update like following code:

class Books extends Controller{

 	function Books(){
 	function main(){
  		$menu = new MyMenu;
  		$data['menu'] = $menu->show_menu();
 	function input(){
  		$menu = new MyMenu;
  		$data['menu'] = $menu->show_menu();	

If you still don't understand about this code, please read this tutorial series.

Next, open "books_menu.php" within CodeIgniter\system\application\views. Replace all code with:

<?php echo $menu; ?>

Point your browser to http://localhost/CodeIgniter/index.php/books/main

codeigniter simple menu library

Series this article:
CodeIgniter - Form: Creating Skeleton
CodeIgniter - Form: File Structure
CodeIgniter - Form: Creating Layout Code
CodeIgniter - Form: Creating Menu Library
CodeIgniter - Form: Putting Text at Header and Footer
CodeIgniter - Form: Centralizing $data
CodeIgniter - Form: Adding CSS
CodeIgniter - Form: Creating Form HTML
CodeIgniter - Form: Creating Beautiful Form Code
CodeIgniter - Form: CodeIgniter HTML Style
CodeIgniter - Form: Preparing Table at Database
CodeIgniter - Form: Creating Insert Data
CodeIgniter - Form: Creating List Data Use Table Library
CodeIgniter - Form: Creating Table List Without Table Library
CodeIgniter - Form: Using URL Helper
CodeIgniter - Form: Showing Single Data for Form Edit
CodeIgniter - Form: Something Happen With Form Parameters
CodeIgniter - Form: Updating Data
CodeIgniter - Form: Deleting Data

Bookmark and Share Tag: CodeIgniter, framework, MVC, library, menu Category: PHP Framework Post : February 15th 2008 Read: 183,956


blog comments powered by Disqus