How do I find the page number for a comment in a paginated system?

I have a pagination comment system, 10 per page. I want to make comments on the comments. I would like the script to find out which page the comment is on, without specifying the page number and without excessively large requests or processing. So, for example, the link to the comment on page 5 will open the browser on the comment page 5 and #, scroll to the comment (as opposed to showing comments on its own page).

I was thinking of using 2 mysql queries, one SELECT all CommentId and then php array_searchto find the position in the result, and from this I could calculate which page it will be on. The second request will receive the required comment page. This seems rather ineffective, although especially if there are a lot of comments to pull out.

Can anyone suggest a simpler or more efficient way?

+3
source share
2 answers

All this can be done quite easily using SQL, considering that comment_id- this is some form of auto-increment number, this should return the exact page:

SELECT FLOOR(COUNT(*)/$comments_per_page) FROM COMMENTS 
   WHERE ENTRY_ID = $entry_id AND comment_id < $comment_id;

, " " ( -). , $comments_per_page - , ( ), $entry_id /, , $comment_id - , .

+3

, , MySQL. perma-link comment_id, - :

select
    count(*)
from
    comments c
where
    comment_id < $comment_id
and
    topic_id = $topic_id -- or whatever

PHP , .

+3

All Articles