Displaying a magazine
Video: Displaying a magazineEarlier 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.
Viewers: in countries Watching now:
Speed up your development with CodeIgniter, a fast and powerful PHP web application framework. Author Jon Peck shows how to build a magazine cataloging system while describing how to use a MVC (Model-View-Controller) framework like CodeIgniter.
Starting with the what and why of CodeIgniter, Jon introduces key concepts such as the MVC pattern and libraries by demonstrating how to create static pages, then storing and displaying magazine info in a database. Advanced topics like classes and helpers are explored to validate user input, upload files, and much more. By creating a complete system, you'll have the foundation to build your own applications with CodeIgniter.
- What is CodeIgniter?
- Creating a static page controller
- Generating output with a view
- What is a model?
- Saving data with Active Records
- Creating forms
- Validating user input
- Listing records in tables
- Uploading images
- Viewing and deleting records
Displaying a magazine
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.
There are currently no FAQs about Up and Running with PHP CodeIgniter.