×

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 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.

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.

CodeIgniter Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

5 minutes read.

Directory Helper Codeigniter

Directory Helper CodeIgniter: provides a directory helper function that works with the directory. It shows the structure of the file, hidden file in the folder, and the folder in...

3 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.

CodeIgniter Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities...

12 minutes read.

CodeIgniter Text Helper

The text helper contains the various function that is used to operate with text such as define a word limit and characters limit, conversion of word from ASCII to entities, etc. Loading the text...

4 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

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.

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.

CodeIgniter Security Class

Security Class The security class provides various function that helps to create a secure application and process input data securely to the application. This class is preloaded in the CodeIgniter application, so you...

4 minutes read.

HTML Table Class CodeIgniter

HTML Table Class The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table. Loading an HTML Table Class...

8 minutes read.

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.

How to remove an index.php file from the Codeigniter URLs?

As we know, a URL in CodeIgniter is designed in such a way that it would be search engine and human friendly, too, rather than using a standard approach to create a...

2 minutes read.

CodeIgniter Helpers

The Helpers (functions) are stored in the Helper file. It helps CodeIgniter to perform different tasks. The helper file is the collection of many functions that is used to perform specific task. It...

3 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.

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.

CodeIgniter HTML Helper

The HTML helper function is used in CodeIgniter to provide various functionality, such as headings, images, links, list tags, etc. Loading the HTML Helper Before using the html helper function, you must load the...

7 minutes read.

CodeIgniter Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

12 minutes read.

CodeIgniter User Agent Class

User Agent Class The CodeIgniter provides a user agent class that helps to identify and collect information about the browser, mobile devices, or robots visiting your site. Besides this, it is also used...

4 minutes read.