Frontpage web hosting - BLOG DESIGN SOLUTIONS database. $myposts becomes false after
BLOG DESIGN SOLUTIONS database. $myposts becomes false after there are no more posts to show, and at this point the while expression knows to stop looping. $post_id = $myposts[”post_id”]; $title = $myposts[”title”]; $dateattime = $myposts[”dateattime”]; echo “
\n”; Inside the do-while loop, the blog post information is extracted from the $myposts array and used to display a list item for each post. This list item links through to addpost.php, appending the post_id in the query string as you had to do in your browser earlier. Note that the post date element is accessed using the dateattime alias created in the SELECT statement. If the alias hadn t been created, the formatted date would be accessed using this rather clumsy code: $dateattime = $myposts[”DATE_FORMAT(postdate, ‘%e %b %Y at %H:%i’)”]; Enter http://127.0.0.1/cms/ in your browser and you should see the list of blog posts currently in your database. The resulting page should look something Figure 7-9. Figure 7-9. Your CMS homepage showing a list of all blog posts in the database Deleting a post The final operation required for your CMS is the capability to delete unwanted posts. This functionality will be built into your index.php page by adding a delete button next to the link for each post and adding a delete script. First, insert the delete link into each list item by amending your code as follows: echo “
\n”; This link sends delete=post_id as the query string so the delete script will know which post to delete. I have also added a JavaScript confirmation to help prevent any unwanted deletions because MySQL has no built-in undo functionality. Here s the delete code; insert it just after the database connection. That way, a deletion will occur before the SELECT query, and hence the post list will be updated correctly: