简体   繁体   中英

PHP & MySQL display problem

When I do pagination for my comments my numbering for my comments starts over on the next page how can I fix this so my count of the comments wont start over for my comments? When I leave out pagination my numbering of my comments works fine. Im using PHP & MySQL.

My counting for my comments is simple

$comment_num = 1; //number comments posted

How I display the numbered comment.

echo '<div>' . $comment_num++ . '</div>';

Display output without pagination.

Comment 1
Comment 2
Comment 3
Comment 4
Comment 5
Comment 6

Display count with pagination set to display 2 comments at a time.

Comment 1
Comment 2

Next pagination page with different comments.

Comment 1
Comment 2

I'm assuming that you are running your query with a LIMIT and offset. Store the offset in a variable then use this:

$query = "SELECT * FROM yourtable LIMIT $offset, 2"

...

$comment_num = $offset + 1;

Do you always show a fixed number of comments on a page? If so, you can start the numbering of a page at

( ( page_number - 1 ) * no_of_comments_on_page ) + 1

Say you show ten comments on a page, for page one you'd get: (0 * 10) + 1 = 1 for page two you'd get ( 1 * 10 ) + 1 = 11 and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM