BlogTutorial:Delete Entry
From Aspen Documentation
Files we'll be editing in this step:
- modules/Blog/Blog_Admin.php
- modules/Blog/templates/index.tpl.php
Delete Method
First, we need to write our delete function. This will simply accept a record id and delete it. If successful, it will redirect to the list page.
public function delete($id = false){ if($this->APP->model->delete('blog', $id)){ $this->APP->router->redirect('view'); } }
The first line runs a delete query on the blog table and removes the record matching $id. $id must be the primary key field.
Adding Delete Link
You now simply need to add a delete link to your index template. Just add a new td to your table so that each record will have a delete link. Then paste the following inside that new cell:
<td><?php print $this->createLink('[delete]', 'delete', array('id' => $entry['entry_id'])) ?></td>
As mentioned previously, this function creates a link with text of "[delete]", points to the delete page, and passes the entry id in the url.
We recommend placing a standard javascript onclick confirmation box so you're warned before deleting it. The createLink accepts an argument for it's onclick, or you can just write a standard anchor element using the createXhtmlValidUrl function instead, like:
<td><a href="<?php print $this->createXhtmlValidUrl('delete', array('id' => $entry['entry_id'])) ?>" onclick="return confirm('Are you sure you want to delete this?);">[delete]</a></td>
Note: Wasn't that easy? What's great is that you just did everything the hard way. All od the above code will be generated automatically for you if you create the blog table first, and then run modadmin. However, you now have a much better idea as to how the framework works and you're ready to take on your own project.