Add a link to delete a post if we are viewing it.

Here is the new code.

  <html>
  <body>
      <h1>{$post->title}</h1>
 
      <br/>
      {$post->body|nl2br} <br/>
      <br/>
      Created on: {$post->time|date_format:"%A %b. %e %Y %l:%M %p"} <br/>
 
      <br/>
      <a href="/blog/post/{$post->post_id}">Edit this post .</a> <a href="/blog/delete/{$post->post_id}">Delete this post .</a>
  </body>
 
  </html>

Here is a screenshot of my new view post page.

Create a controller action to delete a blog post.

Here is the code.

application/controllers/BlogController.php

      public function delete($id)
      {
          $post = Zend_Registry::get('factory')->get('Post', $id);
          $post->delete();
          header('Location: /blog');
          exit;
      }

The code does nothing more other than call the business object's “delete()” function and then redirects to the listing page of blog posts.

Create a function on the "Post" business object for deleting a blog post.

Here is the code.

application/models/Post.php

      public function delete()
      {
          $sql =  "delete from posts where post_id = ?";
          $ret = Zend_Registry::get('db')->query($sql, array($this->_id));
      }

Click on a blog post. Then, click on the Delete this post . link. You will be redirected back to the blog post listing page and you should see that blog post is gone.

Most blog systems have comments. Continue on read how to Create a comment for a blog post.

pox-php/delete_a_blog_post.txt · Last modified: 2010/05/09 20:59 by gerard
 
 
© 2010 Straylightrun.net under Creative Commons Attribution
Green hosting by Dreamhost.com | Powered by DokuWiki