Your assignment today is to complete a blog with comments. This should be based off of your assignments from last week, where you created a blog where users were required to register and login before they could post to the blog.
There will be changes to two pages in your blogs.
index.php
The home page will show all the blog posts, as well as all of the comments associated with each blog post. There will also be a link to “Add a comment” to any blog post.
I recommend you study what I wrote about SELECT statements in an earlier post, especially the last bit about joining two tables together. It would be nice if you could join the users and posts tables together when doing a query for all the blog posts to show on the home page. This would allow you to get all the data about the post, as well as the data about the user who posted it in one fell swoop. Something like this:
$myQuery = "SELECT abloombert_posts.*, abloomberg_users.username FROM abloomberg_posts, abloomberg_users WHERE abloomberg_posts.user_id = abloomberg_users.id ORDER BY created DESC";
$result = mysql_query($myQuery); //run query
if ($result) {
//loop through each post
while ($row = mysql_fetch_array($result)) {
//get this post's id, title, body, and username of the user who posted
$userId = $row['user_id'];
$username = $row['username'];
$title = $row['title'];
$body = $row['body'];
$created = $row['created'];
//etc...
}
}
The link to “Add a comment” has to somehow pass the id of the blog post to which the comment should be associated to the comment.php script. This can be done by passing that data in the url of the link. For example
<a href="comment.php?post_id=<?php echo $postId ?>">Add a comment</a>
comment.php
The comment page will have a form that users can fill out to submit a comment. This page should show the user the title and author of the blog post they are about to comment on.
In other words, this page will have to take the post_id that was passed to it from index.php, and do a read from the posts database table to get the information for the post with that id.
You will probably use code similar to this:
$postId = $_REQUEST['post_id']; //get the post id that was passed from index.php
$myQuery = "SELECT * from posts where id={$postId}";
$result = mysql_query($myQuery);
//... loop through each row of the results and get the data about this post
The form where the user fills in the comment will also need to pass the post_id to the script that processes the comment so it can store the post_id in the comments table.
Assuming you have the blog post “id” field in a variable called $postId, the code for the form will probably look something like this:
<form action="process_comment.php"> <input type="hidden" name="post_id" value="<?php echo $postId ?>" /> <label for="comment">Comment:</label> <textarea name="comment" id="comment"></textarea> <input type="submit" value="Post Comment!" /> </form>
Notice the hidden input that has the value of the post’s “id” field. This means that when the user clicks the submit button, the form sends two pieces of data to the process_comment.php script: the post_id and the comment the user entered.
process_comment.php
The script that processes the comment the user entered must take the blog post id, the id of the current user, and the comment the user entered, and store this info in a new row in the “comments” table.
To get the necessary data, you will need to retrive it from the $_REQUEST and $_COOKIE variables. The post_id and comment will be in the $_REQUEST variable, and the user_id will be in the $_COOKIE variable, assuming you stored it in a cookie when the user logged in.
$userId = $_COOKIE['user_id']; $comment = $_REQUEST['comment']; $postId = $_REQUEST['post_id'];
So make sure you are storing the user’s id in a cookie when they login, or else this obviously won’t work.
Once you have that info, put together a SQL query that inserts this data into a new row in the “comments” table.
$myQuery = "INSERT INTO abloomberg_comments (user_id, post_id, comment) VALUES ({$userId}, {$postId}, '{$comment}')";
$result = mysql_query($myQuery); //run the query
Then redirect the user back to the home page.
header("Location: index.php");
No related posts.

