views:

73

answers:

3

I realize this is somewhat of an abstract question that has several answers, but I am at a loss as to where to begin. I want to have a separate comments area for each of my blog posts. Should I just set up a different table for the comments for each entry manually every time I update the code to include the latest entry?

+2  A: 

Create a new table for the comments with a structure similar to (of course you can customize it to your needs):


Comments
    id INT NOT NULL auto_increment,
    blog_id INT NOT NULL,
    author_id INT NOT NULL DEFAULT 0,
    comment text NOT NULL,
    added_date DATETIME NOT NULL

The author_id is linked to the users table for logged in users, 0 for an anonymous user. Everything else should be self explanatory I hope.

Brad F Jacobs
Shouldnt `blog_id` be `post_id` or something?>I want to have a separate comments area for each of my **blog posts**
Svish
My reply was just a loosely typed setup. Like I said, it should be self explanatory. If you have your blogs setup as "posts" then yes, it would be post_id.
Brad F Jacobs
A: 

I'm not sure what you're quite getting at... but it sounds like you want to have comments specific to each post. If that's the case, just simply make a field in the comments table for "post_id" or something similar. Then on each post page just use a SELECT statement to grab comments for that particular post_id.

Patrick
A: 

Simply have a database table storing the post ID in one of the fields. Something similar to the following:

CREATE TABLE blog_comments (
  id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  author_id INT(10) UNSIGNED NOT NULL DEFAULT '0',
  post_id INT(10) UNSIGNED NOT NULL,
  comment TEXT NOT NULL,
  added_on TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
);

Then you can simply query for post comments like thus:

$pid = 13; // or whatever your post ID is; could be a $_GET value
$sql = "SELECT * FROM comments WHERE post_id = '$pid' ORDER BY added_on DESC";

Of course, be sure to sanitize the above query, as you don't want any one passing what they feel like for the value of $pid. You need to make sure it's a number, and a number only.

Martin Bean
For example `$pid = (int) $_GET['pid'];`
Svish
How do I design my html form to send a value for my post ID variable that I choose? I know that I need to make the method="get" but I don't want a text area created, I want to pass a number or a variable that contains a number.
Jimmy
With an hidden field. `<input type="hidden" name="pid" value="<?php echo $post->id; ?>" />` where `$post->id` is where you would echo the actual post ID using the method you're already using to fetch post data from the database.
Martin Bean