FOXvsNBC
FOXvsNBC

Reputation: 21

How to number comments using PHP & MySQL

I was wondering how can I number my comments using PHP & MySQL and keep the correct comment number when using pagination? A brief example or tutorial would help. Thanks

Example output.

COMMENT 1
COMMENT 2
COMMENT 3

Upvotes: 0

Views: 114

Answers (3)

mck66productions
mck66productions

Reputation: 11

What about identifying and labeling with timestamps? Just a thought... Using seconds would probably be enough to get unique numbers, unless the volume is huge... Then if any comments were removed, the id#s for the remaining comments would be unchanged... Like I said... just a thought...

Upvotes: 0

Juan Pablo Califano
Juan Pablo Califano

Reputation: 12333

Your question is a bit cryptic, but assuming you have your query in place, well, I think you already know the initial offset.

Say you have something like this:

SELECT      col_1, col_2
FROM        my_table
ORDER BY    col_3
LIMIT       $offset, $num_rows

$offset + 1 will be the number of your first record (if you want 1-based numbers). Then in your php loop, increment this counter in each iteration.

Upvotes: 0

cHao
cHao

Reputation: 86506

Start your comment numbering at ($pageNumber * $commentsPerPage + 1) (assuming page 0 is the first), and increment it for each comment.

If comments can skip numbers (for example, if you don't want deleting one comment to bump all the other comment numbers), then you might wanna consider storing the comment number in the database with the comment.

Upvotes: 2

Related Questions