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

