Jump to content

How to determine showing?


newbtophp

Recommended Posts

Im having a bit of trouble on calulating the number of showing search results, if I have 20 search results per page, and page number was 1, it should echo 1-20, if the page number was 2 it would echo 21 - 40, if the page number was 3 it would echo 41 - 60... and soo on.

 

My code;

 

<?php

$totalresults = 348;

$perpage = 20;

$page = 1;

?>

 

:-\

Link to comment
Share on other sites

Im having a bit of trouble on calulating the number of showing search results, if I have 20 search results per page, and page number was 1, it should echo 1-20, if the page number was 3 it would echo 41 - 60.

 

My code;

 

<?php

$totalresults = 348;

$perpage = 20;

$page = 1;

?>

 

:-\

 

Use the LIMIT keyword in your query like such.

$qry = "SELECT * FROM tbl LIMIT " . (($page-1) * 20) . ", " . (($page*20)-1);

Link to comment
Share on other sites

$qry = "SELECT * FROM tbl LIMIT " . (($page-1) * 20) . ", " . (($page*20)-1);

 

First query would be

SELECT * FROM tbl LIMIT 0, 19

 

or select everything from table, start at record 0 and get 19 records

 

So let's say were then on page two...

 

The query would then be

SELECT * FROM tbl LIMIT 20, 39

 

The two arguments to limit are record to start counting from, and number of records to return, not which records to start and end with. So the second parameter should always be 20 :)

 

Link to comment
Share on other sites

$qry = "SELECT * FROM tbl LIMIT " . (($page-1) * 20) . ", " . (($page*20)-1);

 

First query would be

SELECT * FROM tbl LIMIT 0, 19

 

or select everything from table, start at record 0 and get 19 records

 

So let's say were then on page two...

 

The query would then be

SELECT * FROM tbl LIMIT 20, 39

 

The two arguments to limit are record to start counting from, and number of records to return, not which records to start and end with. So the second parameter should always be 20 :)

 

My bad ;) I wrote that wrong. Thanks for correcting

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.