Up and Running with PHP CodeIgniter
Illustration by Don Barnett

Viewing and deleting magazines


From:

Up and Running with PHP CodeIgniter

with Jon Peck

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Video: Viewing and deleting magazines

I've been making solid progress but it's time to focus, and put some final touches on this application. There's two major pieces missing. The ability to view magazines, and delete an individual magazine. In some ways, the two pieces of functionality are very similar. In that they both require something to get an issue_id then some kind of action on a loaded model. I'm going to start by adding links to view and delete from the magazine list. Edit the magazine controller and at the top of the index function add a load helper to load the URL helper.
please wait ...
Watch the Online Video Course Up and Running with PHP CodeIgniter
1h 30m Intermediate Jul 11, 2013

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.

Topics include:
  • 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
Subject:
Developer
Software:
PHP CodeIgniter
Author:
Jon Peck

Viewing and deleting magazines

I've been making solid progress but it's time to focus, and put some final touches on this application. There's two major pieces missing. The ability to view magazines, and delete an individual magazine. In some ways, the two pieces of functionality are very similar. In that they both require something to get an issue_id then some kind of action on a loaded model. I'm going to start by adding links to view and delete from the magazine list. Edit the magazine controller and at the top of the index function add a load helper to load the URL helper.

This load, helper, URL. It would be helpful to see whether a magazine has a cover associated with it. So at the end of the 4H loop lets add a column with a simple Y or N if there is a cover Issue issue_cover switch yes or no. I'm going to use the anchor helper function to generate links based on the local URL as a final column in the magaine table. I'll make links to both view and edit. The first argument f anchor is the path which will be magazine/view. And then I'm going to pass it the issue.

Issue_id. The second is the text view. I'll separate them with a simple pipe. Anchor magazine/delete and then add the issue, issue_id" then delete. Save the controller, then switch to the magazine's view. Add the additional headings Cover and Actions. Cover, Actions. Save the view.

Then let's take a look at the browser. Refresh the magazines list and it looks a little bit more functional. Cool, I can see whether or not a cover is associated with a magazine now. And there's links to delete and view. Let's make those functional. Switch back to the id. Go back to the magazine controller. I'm going to start with a new method for view. Public function view. We'll take one argument, the issue_id. And documentation, view a magazine. Type will be an integer.

The routing system will automatically pass the argument so I don't need to make any additional changes on that side. I'll start by loading the HTML helper, I'll need it in a moment, this->load->helper, html. Then I'll load the header itself, this->load->view, bootstrap/header then the models for issue and publication, this->load->model pass it an array containing issue, publication. I'm going to try to load an issue by its id.

Issue equals new Issue. Then issue load issue_id. if an issue can't be loaded show a 404 error using one of the common functions show_404 which will does just that and exit. If not, issue, issue_id, show_404. Otherwise, we have an issue so load the publication.

Publication equals new Publication, then publication->load the issue, publication_id. I'm going to pass both to the magazine view created earlier. This, load->view, magazine, pass in an array. The key issue and publication. Finally show the footer, let me copy the header, make a small change, footer. Save the controller then edit the magazine view.

Right before the closing div, I'm going to add some logic. Php, if, issue as a issue_cover and we'll post the php, show a div with class cover, close the div, indent, php. I'm going to use a function from the html helper called image, which will generate an image tag based on a path.

So echo Image, upload/.and then issue->issue_cover. Make sure the if is closed out, and save. I'm going to switch to the browser to see the view in action. Click View on the item with the cover. Neat, the cover is now rendered. Now to clean it up a bit, I'll implement the last missing functionality, a delete. Go back to the magazine controller. Start a new method called delete. Public function delete, which takes one argument, an issue_id. Always remember the documentation.

Delete a magazine which takes an integer. I don't need any helpers so I'll just start off with loading the header. This load->view bootstrap/header then the model for the issue only. This->load model->array issue, instantiate a new issue, issue equals new Issue and try to load it, issue load issue_id.

Again if the issue can't be found show 404, otherwise delete the issue using the common method delete, issue delete. Show a success message by using a new view magazine deleted which I will create it in just a moment passing just the issue_id, this->load->view magazine deleted an array containing issue_id, issue_id.

Finally show the footer, save the controller then create a new view Named magazine_deleted. This is going to be a one liner, div class equals alert, alert-success magazine number then php echo html_escape Issue_id. Close that out and then the verb which will be deleted. Save the new view and let's test it.

From the browser go back to the list of magazines. And delete the first one without a cover. I get a message that the magazine one was deleted. If I go to the home screen, there's one less magazine to show. That wraps up the base functionality for the magazine catalog system. In this chapter, I discussed how to use a helper to perform tasks, then demonstrated how to use the form helper to save a lot of time by generating HTML forms. I then delved into uploading images using the File Uploading class.

Then finally viewed and deleted magazines. That's a lot of functionality that I've covered throughout this course. So, where to go from here?

There are currently no FAQs about Up and Running with PHP CodeIgniter.

 
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.

* Estimated file size

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 ?

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 preferences from 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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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.