Web Programming TutorialsModels in Code Igniter PART-2

Models in Code Igniter PART-2

In the last section we went through the theory of Models in codeigniter and today we will try using them practically in this Models in CodeIgniter PART-2 tutorial.

We will create a database in mysql and connect it to our application and then perform all manipulations in our model class.

So follow the steps:

  1. Creating database in mysql:
  • To create a database in mysql, open your browser and write http://localhost/phpmyadmin in the address bar.
  • A login page will appear, so put root as the username and the password you have as the password.phpmyadmin_home_page

               fig 1

  • Now click on New option which is on the left side. You will get a page asking you the database name. So provide your database name and then click on Create
  • After clicking on Create button, you will get a message of successful creation of your database.
  • Now you will find your database name at the left side in the list below the New option pointed to in fig 1 (Consider our database name as employees_db).
  • Click on your database name, you will be asked for your table name and the number of columns it will contain.
  • Put the table name say emp_details and no. of columns say 5 and then click on Go
  • You will get the page to provide columns of the table. You will be asked to provide the name, type, and length etc. of the columns. So provide the information of the columns to be created and then click on Save Your table will be created.
  • OR you can write a create table query to create a table.
  • Now our employees_db database with emp_details table is ready.
  • Put some values in the table. I have entered 2 records in the table emp_details.

     2. Providing connection values in the database.php file:

  • We have studied in the last section that we need to provide the username, password and database name in the php file in the application/config folder inside your CodeIgniter folder.
  • So open the database.php file and provide the username, password and database name for the respective array key and then save and close the file.
  • Next thing to be done is to add the word database to the libraries array in the php file.
  • This is automatic connection used by us, similarly you can connect the database manually by providing the values of username, password and database name in the php file in application/config folder and then calling the function $this->load->database() in the constructor of model class or in any function where you want to perform manipulations with database.

       3. Creating a model file:

  • Now let us create a file for the model class in the application/models
  • You can even create another separate folder inside the application/models folder for your model classes.
  • So open a new notepad++ document and save it as emp_model.php in the application/models folder.
  • Write the following code inside the emp_model.php file:
    <?php
    	class Emp_model extends CI_Model
    	{
    		public function __construct()
    		{
    			parent::__construct();
    			//$this->load->database();
    		}
    		
    		public function count_emp()
    		{
    			$num=$this->db->count_all('emp_details');
    			return $num;
    		}
    		
    		public function get_all_records()
    		{
    			$res=$this->db->get('emp_details');  //this returns all the rows from the specified table
    			
    			//return the result
    			
    			return $res->result();
    			
    		}
    	}
    ?>
    

     

  •  The model class name is Emp_model which inherits from CI_Model class.
  • We have two functions in Emp_model class: the first function count_emp() will count the number of employees in the emp_details table of our database employees_db. And the second function get_all_records() will return all the records in the emp_details
  • We have used Active Record functions count_all() and get() to manipulate with the database.

      4. Creating view files:

  • To display the contents of database on the browser we will require view files. So let us create a header and footer template and a content page to display the database table values.
  • First we will create templates: For this create a separate folder named templates in the application/views
  • Now open a new notepad++ document and save it as header.php in the newly created templates folder.
  • Write the following code in header.php file:
    <html>
    <head>
    <title><?php echo $title;?></title>
    </head>
    <body>
    <h1 style="font-weight:bold;font-style:times new roman;background:#D8F781;color:purple;text-align:center;">SoftLogic Software Solutions</h1>
    
  • Save the file and close it.
  • Now open a new notepad++ document and save it as footer.php in the newly created templates folder.
  • Write the following code in footer.php file:
    <div style="background:#D8F781;">
    <em style="color:purple;text-align:center;padding-left:250px;">&copy; SoftLogic@2015</em>
    </div>
    </body>
    </html>
    

     

  • Now we want to create the page in which we want to display the database table contents. So to store the content page create a new folder in application/views folder named as body_pages.
  • Now open a new notepad++ document and save it asphp in the newly created body_pages folder.
  • Now write the following code in emp_info.php file:
    <br>
    	<img src="http://localhost/CodeIgniter/images/work1.jpg" width="225" height="100"/>
    	<img src="http://localhost/CodeIgniter/images/work2.jpg" width="225" height="100"/>
    	<h1>We are a leading software developers in India.</h1>
    	<br><hr>
    	<h2><?php echo $title;?><h2>
    	<h3>No. of employees: <?php echo $no;?></h3>
    	<table border="1">
    	<tr>
    	<th> ID </th>
    	<th> NAME </th>
    	<th> DEPARTMENT </th>
    	<th> DESIGNITION </th>
    	<th> CONTACT NUMBER </th>
    	</tr>
    	<?php foreach($employees as $list){?>
    	<tr>
    	<td><?php echo $list->emp_id;?></td>
    	<td><?php echo $list->emp_name;?></td>
    	<td><?php echo $list->emp_dept;?></td>
    	<td><?php echo $list->emp_desig;?></td>
    	<td><?php echo $list->emp_contact;?></td>
    	</tr>
    	<?php }?>
    	</table>
    	<br>
    

     

  •  In this file we have displayed the title, number of employees whose records are entered in the emp_details table of employees_db
  • Also we have displayed the records from emp_details table in an html table format.

      5. Creating a Controller file:

  • Controller file is an important file which loads all the models and views.
  • So to create the controller for our application, open a new notepad++ file and save it as php in the application/controllers folder.
  • Write the following code in the emp_data.php file:
    <?php
    	class Emp_data extends CI_Controller
    	{
    		public function __construct()
    		{
    			parent::__construct();
    		}
    		
    		public function index()
    		{
    			$this->load->model('emp_model');
    			$data['title']='Employees Details';
    			$data['no']=$this->emp_model->count_emp();
    			$data['employees']=$this->emp_model->get_all_records();
    			
    			$this->load->view('templates/header',$data);
    			$this->load->view('body_pages/emp_info',$data);
    			$this->load->view('templates/footer');
    			
    			
    			
    		}
    	}
    ?>
    

     

  • The controller class is named as Emp_data which inherits from CI_Controller class.
  • It has an index() function written in it which loads the model class and all the views.
  • The Loader class function $this->load->model() is used to load the model class in the controller. Name of the model is provided inside the function as shown below:
    $this->load->model('emp_model');

     

  •  We have assigned some values to the $data array as shown below:

 

$data['title']='Employees Details';
$data['no']=$this->emp_model->count_emp();
$data['employees']=$this->emp_model->get_all_records();
  •  The title Employees Details is provided as the title in the array. Similarly the no. of employees and their details are obtained by calling the functions written in the emp_model.php file.
  • The functions written in the model class are called as $this->model_class_file_name->function_name.
  • Next whatever data accessed from the database should be displayed in the browser, so for that view files are loaded and the $data array is passed to them if needed as shown below:
    $this->load->view('templates/header',$data);
    $this->load->view('body_pages/emp_info',$data);
    $this->load->view('templates/footer');
    

    6. View the output:

  • To view the output, open your browser and write the following address in the address bar of your browser:

http://localhost/CodeIgniter/index.php/emp_data

  • emp_data is the controller file name.
  • The output is shown below:

final_output

 fig 2

  • In the above figure we can see the title passed in the $data array i.e. Employees Details.
  • We see number of employees i.e. 2 and the records in the emp_details table in the database are also displayed.
  • Similarly, we can perform any operation like insert, update, and delete using the active record functionality.

 

Thus we studied practically the connection and application of database with the model in codeigniter in this Models in CodeIgniter PART-2 tutorial.              

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -