The Code Igniter framework makes it easy for developers to implement a Model-View-Controller design pattern in PHP. This concept can be difficult for newcomers to grasp, so this seven-part series of articles will focus on one aspect of it: views, and the many clever ways you can handle them with Code Igniter. Welcome to the first part.
In the previous section, I explained how to build a couple of static views that will be used later on for generating the header and footer parts of a simple web page. However, the only view file that remains undefined is the one tasked with rendering the main area of this page. In this situation, I’m going to populate this area with content from a MySQL table called “users,” which I also used in some other PHP articles published here at the prestigious Developer Shed network.
As a quick reminder, the structure of this table looked like this:
Now that you hopefully recalled how this example table was filled with some trivial records, it’s time to build the view file that loops over each of them. Here it is:
Undoubtedly, the above view should be fairly easy to grasp for you, since all that it does is iterate over each user fetched from the previous MySQL table and display their first and last names, and their email address, on screen. Before I forget, please save this view under the /application/views/ folder of CodeIgniter, so it can be used later on.
Done? Great. Having created the view file that generates the dynamic section of the sample web page, the next step I’m going to take will consist of defining a simple controller class. As you’ll see in a moment, this controller will be responsible for loading the three views in a sequential fashion, in this way generating the entire web document.
This procedure will be shown in the upcoming section of this article, so if you wish to learn more, please read the next few lines.