phpeveryday.com

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


CodeIgniter - Form: Creating List Data Use Table Library

Tag: CodeIgniter, framework, MVC, database, table, table library   Category: PHP Framework
post: 25 Feb 2008 read: 2,218


CodeIgniter Form Step By Step Tutorial - Part 13: At this post, we will build list of data. We don't build as manual, but, use CodeIgniter table library. It is so simple and easy.

First, update our model (books_model.php within models). Add getall() method like below (line 22-29):


<?
class books_model extends Model{
	
  function books_model(){
	parent::Model();
	$this->load->helper('url');				
  }
  
  function entry_insert(){
    $this->load->database();
	$data = array(
	          'title'=>$this->input->post('title'),
			  'author'=>$this->input->post('author'),
			  'publisher'=>$this->input->post('publisher'),
			  'year'=>$this->input->post('year'),
			  'available'=>$this->input->post('available'),
			  'summary'=>$this->input->post('summary'),
	        );
	$this->db->insert('books',$data);
  }
  
  function getall(){
    $this->load->database();
	$this->load->library('table');
	
	$query = $this->db->query('SELECT * FROM books');	
	$table = $this->table->generate($query);
	return $table;  
  }
	
  function general(){
	$this->load->library('MyMenu');
	$menu = new MyMenu;
	$data['base']		= $this->config->item('base_url');
	$data['css']		= $this->config->item('css');		
	$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';
						   
	$data['title']	 	= 'Title';
	$data['author']	 	= 'Author';
	$data['publisher']	= 'Publisher';
	$data['year']	 	= 'Year';
	$data['years']	 	= array('2007'=>'2007',
	                            '2008'=>'2008',
								'2009'=>'2009');	
	$data['available']	= 'Available';	
	$data['summary']	= 'Summary';
	$data['forminput']	= 'Form Input';
	
	$data['ftitle']		= array('name'=>'title',
	                            'size'=>30
						  );
	$data['fauthor']	= array('name'=>'author',
	                            'size'=>30
						  );
	$data['fpublisher']	= array('name'=>'publisher',
	                            'size'=>30
						  );
	$data['favailable']	= array('name'=>'available',
	                            'value'=>'yes',
								'checked'=>TRUE
						  );
	$data['fsummary']	= array('name'=>'summary',
	                            'rows'=>5,
								'cols'=>30
						  );			
	return $data;	
  }
}
?>

Then, update main() at controller: books.php. Add line 12-13 like below


<?
class Books extends Controller{

  function Books(){
	parent::Controller();
  }
	
  function main(){
    $this->load->library('table');
	$this->load->model('books_model');
	
	$data = $this->books_model->general();
	$data['table'] = $this->books_model->getall();
		
	$this->load->view('books_main',$data);
  }
	
  function input(){

	$this->load->helper('form');  
	$this->load->helper('html');  	
	$this->load->model('books_model');
	
	if($this->input->post('mysubmit')){
		$this->books_model->entry_insert();
	}	
	
	$data = $this->books_model->general();
				
	$this->load->view('books_input',$data);	
  }
  
}
?>	

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



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)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

2 Responses to CodeIgniter - Form: Creating List Data Use Table Library:

I didn't know how it was exactly implemented in the tutorial because the main.php modification is missing in this portion of the tutorial but this is how I updated my main.php I just added in place of the "test list". Please PM me if we have same implementation. thanks

I think you forgot to put a view code in book_main.php that would view the result table from getall(). I just figured this out because the code you gave does not display the results from getall().

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


615
posting