Jump to content

Recommended Posts

So this is how I have my blog table

 

id (primary key) | title | content | date | user

 

here is what am thinking for my comment table

 

id (primary key) | name | date | blogid

 

So if I do an enquiry in PHP where I search the comments table for all comments that match the id number of the blog with my "blogid" and display them in asceding order would that work? Or is there a better way to do this?

Link to comment
https://forums.phpfreaks.com/topic/188129-blog-comment/
Share on other sites

Forum threads; posts, Blog posts; comments, .. they have lots of commonalities for example:

 

A blog post contains a title, publish date, content and has an author. A comment contains a title (Re: ..), approved date, content and has an author. Using this information and our knowledge of SQL allows you to form a more effective application, for example we can create these tables:

 

CREATE TABLE blog_post (
  id integer not null auto_increment,
  author_id integer,
  title varchar(128),
  title_slug varchar(128),
  publish_date datetime,
  content text,
  is_published boolean,
  KEY blog_post_author_id_fk (author_id),
  UNIQUE blog_post_title_slug_un (title_slug),
  PRIMARY KEY (id));

CREATE TABLE blog_post_comment (
  id integer not null auto_increment,
  blog_post_id integer,
  title varchar(128),
  author varchar(32),
  approved_date datetime,
  content text,
  is_approved boolean,
  KEY blog_post_comment_blog_post_id_fk (blog_post_id),
  PRIMARY KEY (id));

 

Our normal 2-step query is now:

 

SELECT title, user.username AS author, publish_date, content
FROM blog_post JOIN user ON blog_post.author_id = user.id
WHERE is_published = true AND title_slug = $slug
UNION
SELECT title, author, approved_date AS publish_date, content
FROM blog_post_comment
JOIN blog_post ON blog_post_comment.blog_post_id = blog_post.id
WHERE is_approved = true AND blog_post.title_slug = $slug

 

Your result now looks like:

 

title | author | publish_date | content
Hello World | ignace | 2010-01-10 15:02:05 | Lorem ipsum dolor sit amet consectetuer adipiscing elit..
Re: Hello World | robin | 2010-01-10 15:59:01 | Wow, great article LOVE IT!!

 

PHP:

 

$blogPost = mysql_fetch_array($result, MYSQL_FETCH_ASSOC);
..

while ($comment = mysql_fetch_array($result, MYSQL_FETCH_ASSOC)) {
..

Link to comment
https://forums.phpfreaks.com/topic/188129-blog-comment/#findComment-993606
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.