Display Post #

I just added the ability to Comment on Articles on my website, and would like to place “Post #___” in the upper right-hand corner of each Comment.

What is the best way to do that?

It would seem to me that for a given Article, I want to somehow loop through all of the Comments and display an incremented #.

Or do I do something in the Query that originally displays my Comments?

Debbie

Another possibility is adding a column to your table, and store the number there. That way, if you remove a comment, the other comments maintain their number.
For example, there are 5 comments. I post the 6th, saying "I don’t agree with comment #4 because…). Then you for some reason remove comment #3. That means comment #4 becomes #3, and #5 becomes #4, and my comment about comment #4 makes no sense anymore.

Wow! You make a good point!! :eek:

I just added a variable before my loop and then incremented the counter as I loop through and display each Comment.

That seems to work well enough, but your point is making me re-think this.

Anyone else?

Debbie

Hi Debbie,

I fully agree with guido2004, By allowing the Comments that exist in the database to retain their id then bookmarking the comment is less likely to break in the future.

Regards,
Steve

What does SitePoint do? (I suppose they never delete Posts.)

So back to Guido’s suggestion, when someone adds a new Comment, I would just find the maximum “Post #”, increment by 1, and then store that in a field called “post_no”? Is that what you two are proposing?

Debbie

If appropriate we do :D. What vB (the forum software SPF uses) does is stores a unique id for each post, which is used in quoting so that the posts are always related to each other, but displays a “relative” post number on each post (the # in the top right of each post) which is counted by the page.

Don’t ever do that - if you’re going to use a number, then use the autonumber functionality inherent to the database you’re using. If you do a post count and increment, you run into the possibility that two people could post at the same instant, and end up with a duplicate (or a fatal error). Letting the dbms handle that resolves that problem.

As for how to display them - do you want to display chronologically (that word sucks to spell - ackward for some reason), or do you want a threaded view?

If you want chronologically, then you do a relative count from the first displayed.

Post 1: 1/1/2012
Post 2: 1/2/2012 <- delete this one
Post 3: 1/3/2012
Post 4: 1/4/2012

becomes:

Post 1: 1/1/2012
Post 2: 1/3/2012
Post 3: 1/4/2012

If you want threaded, then you don’t need a post number at all:


Post: 1/1/2012
     Post: 1/3/2012
Post: 1/2/2012 <-- delete this post.
     Post: 1/5/2012
Post: 1/4/2012
   Post: 1/4/2012
      Post: 1/5/2012
     Post: 1/5/2012

becomes (since you should lose the post’s reply as well…)


Post: 1/1/2012
     Post: 1/3/2012
Post: 1/4/2012
   Post: 1/4/2012
      Post: 1/5/2012
     Post: 1/5/2012

Dave,

I am allowing members to post a Comment after one of my Articles. I am not allowing multi-level threads at this point. Everything is just a long list of Comments, based on “Post Date”.

You sort of lost me in your response…

Yes, I do use an AutoIncrement ID for each Comment.

But to ServerStorm’s point, if I ever did delete a Comment, then since I am just using code at the “Presentation Level” to assign “Post #'s”, the number would get messed up.

If I deleted Coment #3, then this…
1
2
3
4

…would become this…
1
2
3

Where Comment #4 is now displayed as Comment #3

It would look awkward to display the “commentID” since that could be gigantic.

I see your point about not wanting to store Comment #'s/Order in the database, but to ServerStorm and Guido’s point, you DO need to lock in the Original Comment # because if others say, “I disagree with what Dave said in Comment #4” then that needs to remain constant?!

Sorry if I am not understanding your response.

Debbie

Hi Debbie,

DaveMaxwell brings up a good point. Even the way your doing it the ‘same time’ post could occur (I’ve seen this in my own PHP apps) and throw an error an even (if it happens just right) corrupt the db. I don’t want to speculate, but at this stage of your site/db design it may not be too difficult to add an auto-number column in your comments table and then it becomes a non-issue when things are deleted and you don’t get the repeat (small) overhead to recalculate the post numbers; if your site ever gets big this will matter.

My comment earlier is based on the fact that I often bookmark something that looks like this http://www.somedomain.com?id=1346792&comment=3

if comment 2 was ever deleted then if comment 3 was the last comment then I’d get a 404 otherwise I would get what used to be comment 4, just not the information I saved.

Yes, I remembered the SP forums working like that too, but apparently I was wrong (or things have changed).

I was convinced that the SP forums worked that way, but I was wrong. So I won’t say things like “I disagree with what Dave said in Comment #4” in my posts anymore.
The problem is, as Dave pointed out, that you can’t prevend problems from happening in case of simultaneous posting, because you can have only one autoincremental column in a table, and that column will be the comment id.

I just added a variable before my loop and then incremented the counter as I loop through and display each Comment.

I’d say you have found the right solution.

Sorry, but I disagree with your premise here - to lock in some sort of arbitrary number just to allow someone to comment like that is clumsy, IMO. Even SP doesn’t try to do that (and there are those that reference back to “post #” in their quotes, though more just use the reply with quote button). A better option would be a mechanism which does a quote of the post/comment. This allows the user to reply to a specific point without having the developer having to worry about trying to keep posts together.

As for your worry about the numbering - what you listed is correct. If post#3 is deleted, then post #4 becomes #3 and so on. The only issue would be the one you listed where someone could refer to a post no longer there, but unless you’re going to be overzealous and delete all comments you don’t like instead of the spammish ones, then there’s nothing really to worry about…

I just checked a number of older SP forum comments bookmarks and they no longer work, but I do know that they used to do this. When these bookmarks were fresh, I used them somewhat regularly.

Dave, maybe correct me if I am wrong, but I don’t see a problem with having an auto-numbering column that keep tracks of comments. One could have a post table with auto-number post_id, and a comments table with auto-number comment_id and a comments2post mapping table that matches post_id with x number of comments.

If a comment is deleted then the post no longer references it - no problem. The remaining comments still have there same ids and so any links to the comments are retained. Then if people want to book mark them, then go for it.

Steve

Valid point.

Guess I’ll have to try and learn how to add a Quote feature to my Comments section.

As for your worry about the numbering - what you listed is correct. If post#3 is deleted, then post #4 becomes #3 and so on. The only issue would be the one you listed where someone could refer to a post no longer there, but unless you’re going to be overzealous and delete all comments you don’t like instead of the spammish ones, then there’s nothing really to worry about…

Well, for now I have to approve all Comments before they are posted, so that would solve most of these issues.

Thanks,

Debbie

Well, this is a whole other topic unto itself.

I think I have been bad on this topic in that I only use Sessions and Post to pass data. Although, I’ve never had issues Bookmarking things.

Dave, maybe correct me if I am wrong, but I don’t see a problem with having an auto-numbering column that keep tracks of comments. One could have a post table with auto-number post_id, and a comments table with auto-number comment_id and a comments2post mapping table that matches post_id with x number of comments.

Not sure what you are calling a “Post” and “Comment”? To me they are the same.

I could do a modification of that and have my “commentID” stay an auto number and then have a corresponding “viewableCommentID” that starts at one so even though it would stay sequential, it it better to have…

1
2
3
5
7
8

versus

3097
3098
3101
3104
3105

and so on…

If a comment is deleted then the post no longer references it - no problem. The remaining comments still have there same ids and so any links to the comments are retained. Then if people want to book mark them, then go for it.

Steve

I’m not following your Bookmarking thing…

All of the Comments would be below an Article with no threading offered. So a Bookmark to “How Postage Meters Can Save You Money” should be unaffected by how the Comments are handled, right?!

Debbie

You are correct, and perhaps that’s where I confused DD. If you look at the post indicators here (#11, #12, etc), you’ll see that there is a link which goes to that specific post based on the postID, even though it displays as a relative number. This serves the human readable purpose of knowing where a comment/post is in a conversation, with the stability of being able to ties posts together as need be, as the id for a post should never change, though the relative number could change if a post earlier in the thread is deleted/moved.

You’re overthinking things and making them too complex - see my previous post where I tried to explain it better. There’s a difference between a permanent indentifier and a relative identifier. A permanent identifier is used so that a record can always be found, regardless of what happens to it (it’s moved, re-ordered, etc). A relative identifier is a human-readable indicator which will make sense to the user when they view it - i.e. the #12, #13 displayed in the top right corner of each post here…

Hi Debbie,

I am thinking this:

Posts Table (Simplified)
[TABLE]
[TR]
[TD]id[/TD]
[TD]post[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]I think that the problem is …[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]I like Kites, what do you …[/TD]
[/TR]
[/TABLE]

Comments Table (Simplified)
[TABLE]
[TR]
[TD]id[/TD]
[TD]comment[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]The problem is not what you think…[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]I also like Kites, in particular…[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]No I agree with your view of the…[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]What the F@ck you DINK!!![/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]I build my own kites see my blog …[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Wow that is a big problem …[/TD]
[/TR]
[/TABLE]

Comments2Posts (Simplified)
[TABLE]
[TR]
[TD]comment_id[/TD]
[TD]post_id[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]1[/TD]
[/TR]
[/TABLE]

Then this will get all your comments related to each post


SELECT
   c2p.post_id
   ,c2p.comment_id
   , c.comment
FROM
  comments as c
INNER JOIN comments2posts as c2p
  ON c2p.comment_id = c.id
Order By 
  post_id, comment_id


SELECT
  id
  , post
FROM 
  posts
Order By 
  id;

Then:[LIST=1]
[]loop through these comments and put the post_id, comment_id, and comments into an array. After doing this you would have an array or comments arrays
[
]create a loop to read your posts. Inside this loop you first output the post (or put it into an $html variable) and then loop through comments and where comment’s post_id = the post’s id you then output this comment (or append it to your $html variable).
[/LIST]By linking your comments this way you don’t have to worry about re-ordering or if you implement a way where a user can bookmark an individual comment (like SP currently lets us bookmark a single post by selecting the post id and then bookmarking).

Hope this helps.

Steve

ServerStorm,

I’m not following your terms…

A “Thread” is this entire discussion (e.g. “Display Post #”)

A “Post” is each individual message (e.g. What I am typing now) related to the “Thread”

In my world…

I have an “Article”. You can figure that out?!

Then Users can make “Comments” to that “Article”

So in my situation, there is no concept of a “Thread”, although an “Article” is conceptual the same.

A “Comment” is identical to a “Post”.

Sorry, but your (non-standard" use of the term “Post” got me really confused…

Thanks,

Debbie

Hi,

So my ‘Post’ is your ‘Article’ and my ‘Comment’ is your ‘Post’, so the example i gave, the posts table would become the articles , the comment table would become posts, and the comments2posts table would become articles2posts. All the other logic would apply the same way.

I whipped this together and read your posts too quickly, so sorry about the confusion.

Steve

No one is hearing me… (:

I am responding to the Post Label #14 which is presumably relative, and postID=5083870 which is absolute and presumably the primary key for your Post.

postID never changes.

But if Post Label is just a calculated, relative number 1, 2, 3, 4,… then if an Admin deleted Post Label #13, then your Post #14 becomes Post Label #13 which is a problem if I am referring to it here.

It would look silly to have the Post Label = postID = 5083870 although you could do that.

Because we all agree that postID needs to remain a primary key…

In the context of this Forum…

I could use an AutoIncrement field called “Post Label” which is assigned “1” for each new “Thread”, and then “Posts” would go 1, 2, 3, 4,… If a Post was deleted, the natural, physical ordering would remain, but there would be gaps like 1, 2, 4,… but at least the numbers would be READABLE!!!

In the context of my website…

I could use an AutoIncrement field called “Post Label” which is assigned “1” for each new “Article”, and then “Posts” would go 1, 2, 3, 4,… If a Post was deleted, the natural, physical ordering would remain, but there would be gaps like 1, 2, 4,… but at least the numbers would be READABLE!!!

In summary, that way you have a unique, primary key describing the Post that the database can use (e.g. 5083870) and you also have a unique key describing the Post that is more user-friendly (e.g. 1, 2, 3, 4, __, 6, 7, __, 9, 10,…)

Debbie

Steve,

You are a beautiful person, but it is very UNCOOL to not use the terms “Thread” and “Post” properly on a website that consists almost entirely of “Threads” and “Posts”?! Grrr… :unhappy:

Debbie