Fetch Data From Database and Show in Tabular Format in Codeigniter – Info PHP

Introduction

This tutorial is intended to introduce you to the CodeIgniter framework and the basic principles of MVC architecture. It will you how a basic CodeIgniter application is constructed and for starters, I will assume that you already have installed CodeIgniter and you have a MySQL installed on your webserver.

In this article, I will show you how to fetch data from s database and show it in a view in a tabular format in codeigniter.

See the following points.

  1. Create Table in MySQL database

    My database name is “abc” and my table name is “country”.

    1. CREATE TABLE country  
    2. (  
    3.    Country_id int,  
    4.    country_name varchar(255)  
    5. );  

    Here is my table structure;

  2. Database connectivity

    You must first edit the file database.php that can be found in application/config/ folder. The important changes are these:

    1. $db[‘default’][‘hostname’] = ‘localhost’;  
    2. $db[‘default’][‘username’] = ‘root’;  
    3. $db[‘default’][‘password’] = ;  
    4. $db[‘default’][‘database’] = ‘abc’;  
    5. $db[‘default’][‘dbdriver’] = ‘mysql’;  
  3. Autoload the database

    If your application uses the database very much then you need to change the autoload.php that can also be found inside the application/config/ folder.

    1. $autoload[‘libraries’] = array(‘database’);   
  4. Config file

    By default, CodeIgniter has one primary config file, located at application/config/config.php.

    1. $config[‘base_url’] = ‘http://localhost/CI/’;   

    CI is the folder where my application is present.

  5. The Model

    In the Models we have the CRUD operations. I will create a model named select.php.

    1.    class select extends CI_Model  
    2.    {  
    3.       function __construct()  
    4.       {  
    5.   
    6.          parent::__construct();  
    7.       }  
    8.   
    9.       public function select()  
    10.       {  
    11.   
    12.          $query = $this->db->get(‘country’);  
    13.          return $query;  
    14.       }  
    15.    }  
    16. ?>  
  6. The Controller

    I will create a model named home.php.

    1.    class home extends CI_Controller  
    2.    {  
    3.       public function index()  
    4.       {  
    5.   
    6.          $this->load->database();  
    7.   
    8.          $this->load->model(‘select’);  
    9.   
    10.          $data[‘h’]=$this->select->select();  
    11.   
    12.          $this->load->view(‘select_view’$data);  
    13.       }  
    14.    }  
    15. ?>  
  7. The view

    To display the output we need a view named “select_view”. The code will go something like this:

    1. “http://www.w3.org/1999/xhtml”>  
    2.      
    3.       “Content-Type” content=“text/html; charset=utf-8” />  
    4.       Untitled Document  
    5.      
    6.    
      “1”>  

    7.       
    8.   

    9.          
    10.   

    11.             
    12.   

    13.             
    14.   

    15.          
    16.   

    17.          
    18.          foreach ($h->result() as $row)  
    19.          {  
    20.             ?>
    21.   

    22.             
    23.   

    24.             
    25.   

    26.             
    27.   

    28.          
    29.          ?>  
    30.       
    31.   

    32.    
    33. Country IdCountry Name
      echo $row->Country_id;?>echo $row->country_name;?>

        

    34.   
    35.   
    36.   
    37. Output

      program output

    38. Article Prepared by Ollala Corp

You might also like
Leave A Reply

Your email address will not be published.