phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


CodeIgniter - Form: Creating Menu Library

Tag: CodeIgniter, framework, MVC, library, menu   Category: PHP Framework
post: 15 Feb 2008 read: 4,922


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:

<?php
class MyMenu{
 	function show_menu(){
  		$obj =& get_instance();
  		$obj->load->helper('url');
  		$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(){
  		parent::Controller();
 	}
	
 	function main(){
  		$this->load->library('MyMenu');
  		$menu = new MyMenu;
  		$data['menu'] = $menu->show_menu();
  		$this->load->view('books_main',$data);
 	}
	
 	function input(){
  		$this->load->library('MyMenu');	
  		$menu = new MyMenu;
  		$data['menu'] = $menu->show_menu();	
  		$this->load->view('books_input',$data);	
 	}
}
?>	

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

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Joomla: Fast Road to Understand Component Programming
Chart: How to Build Cool Animation Real Time Chart
Email: Send Attachement Mail
SMS : Sending SMS with PHP and ActiveXperts (Part 1)

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


624
posting