ad

Monday, 7 March 2016

Codeigniter - How to fetch data from database and display

Show Data form Database to user
You must know some basics of Codeigniter. Please read these two tutorials:
http://www.webdevtown.com/2016/02/mvc-model-view-controller-seriously.html
http://www.webdevtown.com/2016/02/codeigniter-beginners-series-lesson-2.html

Once you've gone through these two tutorials it's time to learn to retrieve information from database and show it on page. We'll just get all the records off database and show it.

Connect
First make sure to connect to database in '/var/www/application/config/database.php'

Set route
Once you've connected lets set default route to 'show_data'. Write $route['default_controller'] = 'show_data'. So whenever use loads your website default controller will run.

Create controller
Create a file named /var/www/application/application/Show_data.php make sure it starts with capital letter. Lets create a controller class. This class will run when user opens your site main page, if no method name is given in URL index method will be called.


PHP:
class Show_stats extends CI_Controller{
        // This is default method that will run if no method name is given in url.
        public function index(){
        }
}
 

Model
Lets create model which will retrieve data from database. Create file /var/www/application/models/Stats_model.php and create a new model class and load database using constructor so it's loaded as soon as class is instantiated.

PHP:
class Stats_model extends CI_Model{
        // We're loading database connection here.
        function __construct(){
                $this->load->database();
        }
}
 

View
Create new file /var/www/application/views/show_data.php. This will is where we will pass data from that we get from model class.

Retrieve data in model.
We created model class earlier but didn't retrieve any data. Lets do it now. Create new function and retrieve all data from database.
PHP:
class Stats_model extends CI_Model{
        function __construct(){
                $this->load->database();
        }

        public function get_stats(){
                // $this->db is an Object that has get method which accepts table name.
                $query = $this->db->get('site_stats');

                // We're returning all the data using $query->result_array();
                return $query->result_array();
        }
}

Loading model into Controller
We created controller earlier but it doesn't do anything. We need to load model, invoke it's method to get the data and the pass it to views to show it to user.
PHP:
class Show_stats extends CI_Controller{

        function __construct(){
                // Running class CI_Controllers constructor.
                parent::__construct();

                // We load the model class.
                $this->load->model('stats_model');
        }


        public function index(){
                // We invoke get_stats method of stats_model class which retrieves data from database, and put them into an associative array $data[].
                $data['stats'] = $this->stats_model->get_stats();

                // We load view and pass it the $data array where we just put the retrieved values.
                $this->load->view('stats', $data);
        }

}
 

Show data in views

Go back to views and run simple foreach statement to show the data to user.

PHP:
<?PHP foreach($stats as $all_stats): ?>
        <b>
                <?PHP echo $all_stats['title']; ?>
        </b>
<?PHP endforeach; ?>
 
That's it guys! now open your localhost and you should see the data. Make sure to popular database table before you do it.

No comments:

Post a Comment