Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Earlier I created a view for presenting content to separate presentation from logic. One of the key features of Codelgniter views is the ability to pass data to the view for rendering in a standardized way. Data can be passed either as an array or an object. I'm going to create a view that renders a magazine, but firs tI'll need to pass data to the view. When data is passed to the view the keys and array or the parameters of an object are accessible as variables with the same name, which can be really handy. Since an issue relates to a publication I'm going to pass both the issue and the publication to the view. So, I'll just use an array.
A more complex implementation that I'm not going to demonstrate but something to keep in mind would be to use lazy instantiation and actually load the publication from the issue model. I'll keep it simple for now. Switch the ID, and make sure that you have the magazine controller open. In the index method, let's wipe everything out, and start fresh. I'm going to start with the variable data containing an array that I'll pass to the view. Then I'll load the publication model. I'll instatiate a new publication, and load the first record by ID. Publication, load, by the ID.
Now that the publication has been populated, i'll put it into the data array. Data, key named, publication, equals publication. Next, we'll prepare the issue by loading the model. This, load, model, issue. I'll instantiate, issue, equals new issue, and load issue load one then I'll also put this issue into the data array, data issue equals issue. Now that all the data has been prepared, I'll render the magazine list field. This, load ,view magazines, then, a new view that doesn't exist yet for rendering a magazine back issue.
For the second argument I'll pass the data array. So, this load view, magazine, and then data. Save the controller, then navigate to the views folder and create a new file for the magazine view called magazine.php. Remember, a view is HTML with a little bit of php, so I'll start off with a div with a class of magazine.
On a new line I'll create the container for the publication name and issue with a classname_issue. Now for a little PHP I'm going to echo the publication name but as a developer I know that I can't ever trust user input. To sanitize the display, I'll use a code ignitor, common function. Common functions are available at any point during execution. In this instance, I'll use HTML escape which is a shortcut for HTML special chars. HTML accepts either an array of input or just a string.
When I pass an array of data to the view, CodeIgniter makes each key available as the variable with the same name. I want the publication name, so I'll access publication and the property publication name. Close parenthesis, add a semicolon, and close the PHP tag. On a new line, I'm going to display the issue number. So, I'll start with a hash tag, then I'll open a PHP tag, then echo, html, escape to issue parameter issue number and semicolon and close out.
And then make sure the div is closed out as well. Finally I'll create a div that'll contain the issues_data_publication, class date. I'm just going to echo, HTML escape the issue, issue date publication for now. Make sure that the date div is closed, and the magazine div is closed. Save, then go back to the browser, and load the Sandbox page.
Now I see my magazines, and my one lonely back-issue of Sandy Shores. It's not very pretty, but it's functional, and shows that I'm able to store and retrieve persistent data, then render it in a standardized manner. In this chapter, I've described what a model is and how it's used. Then modelled the two components of a magazine, the publication and a back issue. Then I demonstrated how to save a magazine using Active Record and added common functionality to a base coding meta class. Then finally rendered a magazine by passing data to a view where I escaped user input so it doesn't break HTML or introduce security risks. In the next chapter, I'm going to use another major feature of Coding Nyder, known as Libraries.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 105523 Viewers
56 Video lessons · 117178 Viewers
71 Video lessons · 86413 Viewers
131 Video lessons · 41310 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.