In Codeigniter Model is Responsible for database related operations like retrieving records from database, inserting records in database, deleting records or updating records in database. So first we need a database. Let’s create one database in phpmyadmin. Open phpmyadmin create database named cidb (Remember we configure the database.php file) and import following sql in it. before you go through this, make sure you have already read Codeigniter Pass Data to View.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
DROP TABLE IF EXISTS `test`; CREATE TABLE IF NOT EXISTS `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(200) NOT NULL, `content` text NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; -- -- Dumping data for table `test` -- INSERT INTO `test` (`id`, `title`, `content`) VALUES (1, 'Home Page', 'Welcome to home page'), (2, 'About Us', 'About Website'); |
Create First codeigniter model
Create a new file in codeigniter model directory and name it testmodel.php. put following code in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php class Testmodel extends CI_Model { function getAllRecords() { $this->load->library("database"); $q = $this->db->get("test"); if($q->num_rows() > 0) { return $q->result(); } return array(); } } ?> |
We created a class with first character capital and name is same as its filename. It extends CI_Model (Codeigniter Model Base Class) base class. Next thing is to create a function called getAllRecords which fetch all records from test table. To access database codeigniter has built in library database which we have to load to access its functionality. $this->load->library(“database”) will load database library in $this->db object =. Generally $this->load->library() function creates object with filename of that library but in database library it is db. If we dont want to put this library load line for database, we can add autoload library in config/autoload.php file. $this->db->get is method in database library which will get all records of specified table from database and returns an query object containing useful information. Query object has num_rows() method which we can use to check how many number of records are returned from database. If any then return array of objects otherwise return empty array.
1 2 |
$autoload['libraries'] = array('database'); $autoload['helper'] = array('url'); |
It autoloads database library and url helper (helper will be discussed later when we use it but for now just autoload the url helper) everytime. so we dont need that $this->load->library(‘database’) line in model.
Next thing is to use codeigniter model in our home controller. so modify code for home controller’s index method as follows:
4 5 6 7 8 9 |
function index() { $this->load->model("testmodel"); $data['records'] = $this->testmodel->getAllRecords(); $this->load->view("homeview",$data); } |
use returned data in view. So modify homeview as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
<html> <head> <title>My Website : Home</title> </head> <body> <?php echo "<pre>"; print_r($records); die; ?> </body> </html> |
Now navigate to http://localhost:8888/ci/ which loads home controller, which calls testmodel. Testmodel fetches records from database and returns array of objects, which then will print_r by homeview.php. So result will be like:
As we can see array of objects are printed on screen now we can loop through array and display it in proper way. So open homeview.php and modify it like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<html> <head> <title>My Website : Home</title> </head> <body> <table> <tr> <th>ID</th> <th>Title</th> <th>Content</th> </tr> <?php foreach($records as $row): ?> <tr> <td><?=$row->id?></td> <td><?=$row->title?></td> <td><?=$row->content?></td> </tr> <?php endforeach; ?> </table> </body> </html> |
now reload browser to see the result and you will get something like this one.
as you can see the data is retried using codeigniter model, which then passed by controller to view and then view loaded the data. I hope you find it useful.
Pingback: Codeigniter CRUD: Day 1 Create Model » CodeRiddles