CodeIgniter - Form: Centralizing $data



CodeIgniter Form Step By Step Tutorial - Part 6: At previous post, we always write code that call library every page at controller. It is not eficient. We can centralize all that always need to display every pages (header and footer too). In this post, we use model for place them.

First, add new function called general() at model. Open "books_model.php" within CodeIgniter\system\application\models. Add like following code:

<?
class books_model extends Model{
	
 	function books_model(){
  		parent::Model();
  		$this->load->helper('url');				
 	}
 	function general(){
  		$this->load->library('MyMenu');
  		$menu = new MyMenu;
  		$data['menu'] 		= $menu->show_menu();
  		$data['webtitle']	= 'Book Collection';
  		$data['websubtitle']= 'We collect 
all title of books on the world';
  		$data['webfooter']	= '© copyright 
by step by step php tutorial';
		
  		return $data;	
 	}
}
?>

Then, update controller: books.php:
<?
class Books extends Controller{

 	function Books(){
  		parent::Controller();
 	}
	
 	function main(){
  		$this->load->model('books_model');
  		$data = $this->books_model->general();
		
  		$this->load->view('books_main',$data);
 	}
	
 	function input(){
  		$this->load->model('books_model');
  		$data = $this->books_model->general();
				
  		$this->load->view('books_input',$data);	
 	}
}
?>
CodeIgniter centralize $data


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


Tag: CodeIgniter, framework, MVC, parameter Category: PHP Framework Post : February 15th 2008 Read: 11,514 Bookmark and Share

blog comments powered by Disqus