CodeIgniter Training 2nd part: Model Classes
Created by Eric Murillo on 2019-06-12
4z.com
Table of Contents
Creating our first Model class
Modifying the Controller and the View
We continue with our guide on the basic concepts of CodeIgniter, in this training, we will understand the operation of the Models and we will finish the example that we had started in the previous delivery. Before continuing, make sure you have completed and understood the previous training.
Preparing the database and configuring CodeIgniter
In the previous training we saw how the Controllers were called, and how data could be sent to the Views in order to show them to the user, but to finish the exercise that we had set, it is necessary to obtain the information from the database.
First, we need to create the structure of the database and introduce some records, for this we will use this SQL script:
Next, we must configure CodeIgniter so that it can connect to the database, for this we will go to the "config" folder under "application", and open the file "database.php" and we will look at these lines:
We must modify these values with the necessary to connect to our database, if we have not made any changes to the configuration that comes with MySQL, the user is 'root' and the password is empty (something that should be avoided in an environment of production for bringing security problems). Finally, the parameter 'database' must be 'test'.
We have everything ready to connect to the database, so it's time to work on our first Model. We will create a file called 'Employees_model.php' inside the folder 'Models', and we will write the following:
In the function '__construct' we load the database, so we can access it using $this->db, as we do in the function 'obtain_employees'. In order to retrieve all the entries in the 'employees' table, we execute the function 'get()', which receives as a parameter the table from which we want the records, so this call would be translated as: SELECT * FROM 'employees’. The 'get ()' function is available thanks to the Active Record class of CodeIgniter, here you can see all the options it gives us.
With this we have a link to the database, the following will be to modify the controller so that it handles this data, for it we return to the file 'employees.php' in 'controllers' and we will make the following modifications in the function 'show_home_page':
First, we load the model that we created in the first line, using the function 'load->model' and passing it the name of the model, then we call the function that we had created and we obtain an array with the information of the base of data. Then, we go through the array calculating the annual salary of each employee and copying this data in addition to the name to a new array, which will be the one we send to the View in the '$data' array.
Finally, we prepare the view to show the information in an orderly manner:
If everything went well, we should be able to see a page like the following:
At the end of the training you must submit a report with the steps to introduce data in the DB and how to show this data using a view. You must provide screenshots of each step taken. This includes code and screenshots of the last web view.
https://docs.switzernet.com/3/public/190611-CodeIgniter1/
https://docs.switzernet.com/3/public/190613-CodeIgniter3/
https://docs.switzernet.com/3/public/190614-CodeIgniter4/
https://docs.switzernet.com/3/public/190615-CodeIgniter5/
https://www.apachefriends.org/index.html
* * *
© 4z.com