article

Friday, August 2, 2013

CodeIgniter show record from database

CodeIgniter show record from database

Create Table
CREATE TABLE IF NOT EXISTS `daily` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `date` date NOT NULL,
  `name` varchar(64) NOT NULL,
  `amount` double NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
Insert
INSERT INTO `daily` (`id`, `date`, `name`, `amount`) VALUES
(1, '2012-02-21', 'Kenshin', 5000),
(2, '2012-02-21', 'Naruto', 6000),
(3, '2012-02-21', 'Lufy', 7000),
(4, '2012-02-21', 'Zoro', 8000),
(5, '2012-02-21', 'Franky', 9000);

Controller
application\controllers\welcome.php
<?php 
class Welcome extends CI_Controller {

 function index(){ 
  $this->load->library('table');
  $this->load->database(); 
  $data['records']=$this->db->get('daily');
  $header = array('ID', 'Date', 'Name', 'Amount'); 
  $this->table->set_heading($header);
  $this->load->view('welcome/index',$data); 
 }
}
View
views\welcome\index.php
<div id="container">
 <h1>Welcome to CodeIgniter!</h1>
<div id="body">
 <?php echo $this->table->generate($records); ?>
 </div>
</div>

Related Post