Add a link to edit 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> 
  </body>
  </html>

Here is a screenshot of my new view post page.

Notice that the URL re-uses an action we've already coded, only there is an extra segment that holds the post ID. In fact, there is very little we have to do to adapt our add blog post code to work for edit blog post.

Modify the ''post()'' action to allow for a Post ID.

Here is the code.

application/controllers/BlogController.php

      public function post($id = null)
      {
          if (empty($_POST))
          {
              if ($id !== null)
              {
                  $post = Zend_Registry::get('factory')->get('Post', $id);
                  $this->view->aTitle = $post->title;
                  $this->view->aBody = $post->body;
              }
 
              $this->view->display('blog/edit.tpl');
          }
          else
 
          {
              $validators = array(
                  'aTitle' => array(
                      'NotEmpty',
                      'messages' => 'Title is required'
                  ),
                  'aBody' => array(
                      'NotEmpty',
                      'messages' => 'Body is required'
                  )
              );
 
              $form = new Form($validators, $_POST);
 
              if (! $form->isValid())
              {
                  $this->view->assign($_POST);
                  $this->view->assign($form->getMessages());
                  $this->view->display('blog/edit.tpl');
              }
              else
 
              {
                  $post = Zend_Registry::get('factory')->get('Post', $id);
                  $post->title = $form->getHTMLEntities('aTitle');
                  $post->body = $form->getHTMLEntities('aBody');
                  $post->create_dt_tm = date('Y-m-d H:i:s');
                  $post->save();
                  header('Location: /blog');
                  exit;
              }
          }
      }

There are three modifications:

  1. post() takes in an $id argument which defaults to null.
  2. Upon page load, we check for an ID in the URL. If it exists, we instantiate the post for this ID, and populate the form with it.
  3. Upon a successful form submission, we pass $id into the factory method, so that it will use the ID to construct a new Post.

Create a new view to hold a form to edit an existing blog post.

Let's re-use the add post template. We don't need to change anything here, except for maybe changing the page header. So I won't re-post the template code. Remember that we get the post business object in the controller action if we pass a post ID in the URL. Then the data from this post object is used to pre-populate the form. Here is a screenhost after I've clicked in Edit this post ..

At this point, the form validation works exactly as before, when we coded adding a blog.

Modify the post's ''save()'' method to accommodate editing an existing blog post.

Currently, the save() method only inserts new database records. We'll use a MySQL-specific trick to use one query to insert the new record if it is not there and update the record if it is there. This clause is called ON DUPLICATE KEY UPDATE. Here is the modified function code.

application/models/Post.php

      public function save()
      {
          $sql = "insert into posts
                      (post_id, title, body, create_dt_tm)
                      values
                      (?, ?, ?, ?)
                      on duplicate key update
                      title = ?,
                      body = ?";
 
          $bind = array($this->_id,
                        $this->_data['title'],
                        $this->_data['body'], 
                        $this->_data['create_dt_tm'],
                        $this->_data['title'], 
                        $this->_data['body']);
 
          Zend_Registry::get('db')->query($sql, $bind);
      }

Edit the blog post body or title and hit submit. You will be taken back to the blog listing page. Click on the blog you just modified, and you should see your changes.

What if you really didn't like what you wrote? Read on to see how to Delete a blog post.

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