Welcome back to our CodeIgniter series. After our first two articles, Why Codeigniter Framework is Better than Custom PHP Development and How to install and Configure CodeIgniter Framework, its time to go on coding our first application using the famous framework. In this tutorial we will be coding a simple application using CodeIgniter’s MVC structure and connecting it to a MySQL database.
MVC in CodeIgniter at a glance
Let’s explain how the Model-View-controller structure in CodeIgniter works. To create a new page, you should create a new class in application/controllers folder extending the core controller class CI_Controller. Each controller function could be accessed as a page, like, http://yoursite.com/index.php/ClassName/FunctionName.
To create a model which contains database operations, you should create a new class in application/models folder extending the core model class CI_Model.The model can be loaded into controller using $this->model->load(‘model_name’) function and any model function can be called after loading the model using $this->model_name’->function_name().
Note: The view code is echoed to the controller when you load one. So be careful not to echo anything in a controller unless it is necessary.
You can include helpers which are built in CI in order to help you code less and save effort for rapid development. You can also load built in or third-party libraries. We will get into this in later tutorials.
This was a foundation for CI development. Now, let’s go on coding our first application using CI.
To make a new page we will create a controller called Php_gang_ci with a function called first_app_tut. In this function we load some data from the database and send this data to the view in order to present it to the visitors. Continue reading to see the contents of the functions that we call here.
To make this controller default, you should edit the array value in the application/config/routes.php file as follows. The index() method then will be the homepage for your site.
$route[‘default_controller’] = “php_gang_ci”;
First, we will create a database using PHPMyAdmin or you can use you favorite MySQL administration tool. For this tutorial we will work on a database called php_gang_ci_toturial with a username: phpgang and a password: phpgangpass.
Run the following SQL statement to create a table in the newly created database.
INSERT INTO `php_gang_ci_toturial`.`php_gang_ci_table` (
(NULL , 'phpgang', 'The best php site everr'),
(NULL , 'facebook', 'a social network');
To make you application connect to a database you should enter the connection info to the database.php file in application/config folder.
$db['default']['hostname'] = 'localhost'; // or your hostname if it's different
$db['default']['username'] = 'phpgang';
$db['default']['password'] = 'phpgangpass';
$db['default']['database'] = 'php_gang_ci_tutorial';
Now the database configuration and data are ready. We will create a model class called PHP_gang_ci_model in the application/models folder and create a method called data_grabber. Remember that we called those methods in the controller.
It’s time to make the data readable to the user. We will create the php_gang_ci_view view that we previously called in the controller.
Now it’s done. Yo can check your site using this link http://yoursite.com/index.php/php_gang_ci/first_app_tut and see the results. If you have any questions post a comment and I be available to answer your inquiries.