I have a comment system with pagination, 10 per page. I want to make permalinks to the comments. I’d like to be able to have the script figure out what page the comment is on without having to specify the page number and without excessively large queries or processing. So, for example, a link to a comment on page 5 would open the browser on comment page 5 and #scrolled to the comment (as opposed to showing the comment on it’s own page).
I had thought to use 2 mysql queries, one SELECT all CommentId and then php array_search to find the position in the result and from that I could calculate what page it would be on. A 2nd query would get the page of comments required. This seems quite inefficient though especially if there are a lot of comments Ids to pull.
Could anyone suggest a simpler or more efficient way?
It can all be done pretty easily with SQL, assuming that
comment_idis some form of auto-incrementing number, this should return the exact page:Of course, this assumes “flat comments” (no parent-child relationships). Obviously,
$comments_per_pageis the number of comments which are shown on each page (this may just be a constant),$entry_idrefers to the blog/forum post which the comment was made under and the$comment_idis the comment you are looking for.