Example 3 – the database query by a model rendering results to a view
In this example, we will show you how the CI controller uses the CI model to retrieve data from the database and render it to a CI view.
The URL will be http://ourdomain.com/index.php/user
.
First, we will have to configure the database settings in the configuration file application/config/database.php
.
We should keep the default database settings unchanged, and only change the following configuration parameters:
The model class will retrieve all the user details from the table users
.
For more information on configurations, refer to Chapter 2, Configuration and Naming Conventions.
We will build the following three scripts:
The controller class: application/controllers/user.php
The model file: application/model/usermodel.php
The view script: application/views/userview.php
The controller retrieves the users
list from the database via the model and renders the view with it.
The following is the code sample of the controller:
The view in this example shows the table content received from the controller containing the users
list as defined in the database.
The following is the corresponding rendered view: