| PHP Error Message |
Warning: Invalid argument supplied for foreach() in /home/a3178292/public_html/article.php on line 213
CodeIgniter - Form: Creating List Data Use Table Library
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
