Jump to content

How do you limit what is shown in pagination


blink359

Recommended Posts

Hi there i wanted to know how i could rule out certain rows from being shown in the pagination in this case i want to approve all information before it's posted i tried

$sql = "SELECT * FROM tablename LIMIT $offset, $rowsperpage WHERE approved = 'Yes'";

But it didnt work

 

Any help would be great,

 

Thanks,

 

Blink359

Link to comment
Share on other sites

Well here is example:

+------------------------------------+
| ID  | Name                         |
+-----+------------------------------+
| 1   | Johnny                       |
| 2   | Somebody                     |
| 3   | Someone                      |
+------------------------------------+

From that table:
SELECT * FROM table LIMIT 0,1  // Gives you the first row
SELECT * FROM table LIMIT 1,1  // Gives you the second row
SELECT * FROM table LIMIT 2,1  // Gives you the third row

So you should have your offset set as: ($page_number - 1) * $items_per_page
Then if I now wanted one per page for the first page my offset would be:
(1 - 1) * 1 = 0
For the second page:
(2 - 1) * 1 = 1

If I want two rows per page:
(1 - 1) * 2 = 0
(2 - 1) * 2 = 2
(3 - 1) * 2 = 4

So to make it clear do the math in your PHP script, but this is what your SQL statement theorically consists of:
SELECT * FROM table WHERE approved = 'Yes' LIMIT ((page_number - 1) * rows_per_page), rows_per_page;

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.