Jump to content

Breaking a page into different pages


aashcool198

Recommended Posts

When we open php help forum we have option to go to different pages

such as :

 

Pages: [1] 2 3 4 5 6 ... 2774

 

I want exactly this in my website for the comments page as there are going to be several comments and i can't put all of them on one page!

 

Please Help!

 

Link to comment
Share on other sites

most common way is to send the page number in the url... with $_GET...

then have your sql which gets the comments and also looks for the page number...

 

then based off that page number and the amount of posts you want to display, you'd have the SQL changed depending on this..

i.e. if page was 3 and you displayed 20 posts per page,

$page = ($_GET['pageNumber'] - 1) * 20;

minus one because you want to find the start of the posts needed..?

 

then your sql would be like

"SELECT * FROM comments ORDER BY whatever LIMIT $page, 20"

with limit x, y.. the x variable sets the starting number (remember the first entry in the results list is zero, so 20 is the 21st result...... so if you have limit 5, 10.. you'd get the 6th - 10th results.

 

then you'd need a sql command which also counted how many comments there were for the page numbers..

then i'd divide it by 20 or however many posts per page to work out how many pages you need.

 

$amountOfComments = $amountOfComments / 20;

 

for ( $counter = 1; $counter <= $amountOfComments; $counter += 1) {

echo '<a href="'.basename($_SERVER['PHP_SELF']).'?pageNumber='.$counter.'">'.$counter.'</a>';

}

 

and then the y value tells sql how many results to pull.

 

anyway thats just the basics, theres heaps of tutorials on it that will explain it better than I can, google is your friend

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.