Viewing and deleting magazines
Video: Viewing and deleting magazinesI'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.
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
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.