Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Displaying a magazine

From: Up and Running with PHP CodeIgniter

Video: 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.

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.

Show transcript

This video is part of

Image for Up and Running with PHP CodeIgniter
Up and Running with PHP CodeIgniter

23 video lessons · 5055 viewers

Jon Peck
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Up and Running with PHP CodeIgniter.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.