×

Retrieve data from Database

Retrieve data from the Database

Now we will discuss how we can retrieve data from the database that we have inserted to the employees table of the database in our previous topic “inserted data to the database”.

Retrieve data from Database

The following are the records of the employees table that we have to retrieve in our CodeIgniter application.

     To get the above records, we need to create the following files, as shown below:

Create a controller file Employee_controller.php and save it in application/controller/Employee_controller.php. And write the following program in the controller file.

Employee_controller.php

<?php
 defined('BASEPATH') OR exit( 'No direct script access allowed');
 class Employee_controller extends CI_Controller 
 {
  function _construct()
     {
         parent::_construct(); 
         $this->load->model('Data_Model'); // load a Data_Model 
     } 
 public function index()
 {    
        $this->load->database();  // load the database
     $this->load->model('Data_Model');
     $query = $this->Data_Model->index();  // fetch records from Data_Model
     $records['info'] = null;
     if($query)
     {
         $records['info'] = $query; 
     }
     $this->load->view('fetch', $records); // load a view file
 } 
 }
 ?> 

Create a model file Data_Model.php and save it in application/model/Data_Model.php. And write the following program in the model file.

Data_Model.php

<?php
 class Data_Model extends CI_Model
 {
 public function __construct() 
 {
 parent::__construct();
 }
 public function index() 
       {
         $this->db->select("id, name, email, age, company, mob, country"); /* define the attributes of employees table/ */
     $this->db->from('employees'); // retrieve data from an employees table
         $query = $this->db->get(); 
 return $query->result_array();
       }
 }
 ?> 

Create a view file fetch.php and save it in application/views/fetch.php. And write the following program in the controller file.

fetch.php

<!DOCTYPE>
 <html>
 <head>
 <title> Tutorial and Example </title>
 </head>
 <body>
 <h2> Employee Details </h2>
 <div id="container"> 
 <p> <b><?php echo anchor('Employee_controller/insert_data', 'Add Employee'); ?> </b></p>
 <table border = 1>
     <tr>
         <th cellpadding = "20"> Id </th>
         <th cellspacing = "20"> Name </th>
         <th cellpadding = "20"> Email </th>
         <th cellpadding = "20"> Employee Age </th>
       <th cellpadding = "20"> Company </th> 
         <th cellspacing = "20"> Mobile No. </th>
         <th cellspacing = "20"> Country </th>        
 </tr>
     <?php foreach ($info as $row): ?>
     <tr>
     <td> <?= $row['id'] ?> </td>
    <td> <?= $row['name'] ?> </td>
    <td> <?= $row['email'] ?> </td>
      <td> <?= $row['age'] ?> </td> 
     <td> <?= $row['company'] ?> </td>
         <td> <?= $row['mob'] ?> </td>
         <td> <?= $row['country'] ?> </td>
         </tr>
     <?php endforeach; ?>
 </table>
 </div>
 </body>
 </html> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Employee_controller function; it shows the output as shown below.

Retrieve data from Database

Related Topics

CodeIgniter File Helper

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

7 minutes read.

CodeIgniter Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

3 minutes read.

CodeIgniter Routes

The Routes are used to handle URL requests on the browser. It matches the URL request to the predefined routes in CodeIgniter. Therefore, a file must be predefined as a routes.php file to...

6 minutes read.

CodeIgniter Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

10 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 minutes read.

Calendaring Class CodeIgniter

Calendaring Class The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar...

6 minutes read.

Image Manipulation Codeigniter

Image Manipulation A Codeigniter provides an image manipulation class that is used for resizing, cropping, rotation, and many other manipulation tasks. This class also support three major image libraries such as GD/GD2, NetPBM, and...

11 minutes read.

CodeIgniter Creating Library

As we know, CodeIgniter has a large collection of library functions that are stored in the system/libraries folder. CodeIgniter also provides some additional functionality in library files. For example, if you want to...

3 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

Migration Class CodeIgniter

The CodeIgniter framework provides the migration class that helps the web developers to create and handle databases in a structured and streamlined manner. The migration classes are very useful when more than...

6 minutes read.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.

CodeIgniter String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

7 minutes read.

CodeIgniter Encrypt Class

The Codeigniter provides an encrypt class used to protect confidential data stored on computer systems or data transmitted over a network. It provides two-way data encryption techniques such as Mcrypt PHP extension and...

4 minutes read.

Download Helper Codeigniter

Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc. Loading...

2 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

CodeIgniter URI Class

URI Class The CodeIgniter contains a URI class that is used to retrieve information from URI strings. It also provides suitability to get information from the re-routed segments. The system automatically initializes this...

6 minutes read.

Database Configuration CodeIgniter

In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting,...

10 minutes read.

CodeIgniter Libraries

The library is an important part of the CodeIgniter framework. It has a large collection of libraries files that are used to increase the performance of an application. By default, all CodeIgniter libraries...

4 minutes read.

Structure of Codeigniter

The File structure of CodeIgniter is divided into three parts: ApplicationSystemuser_guide Application -: As the name represents Application, which contains all the main parts of the project like a controller, libraries, view, models, and others....

3 minutes read.

CodeIgniter Views

A View is a presentational part which shows the information to the users. It is a complex web page which contains navbar, header, footer, sidebar, etc. A view file cannot be called directly...

5 minutes read.