Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using LIMIT Missing Last Entries 2

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
Using a simple page navigator that gives basic Page navigation to build the LIMIT clause dynamically seems to give unexpected results. For example, limiting results to 20 records but having a record set containing 23 records, when I select Page 2, it is empty but when I select Page 1 again, there the last three are.

The HTML navigator is something like this with the current page in-linked in square brackets:

Code:
<div class="Pagination">[1]
<a href="/categories/help/sitesearch.php?Page=2" title="To page 2">2</a></div>

. . . and the PHP building the conditional to append to the end of the query is:

Code:
if ($_GET['Page']) :
	$Offset = (20 * $_GET['Page']) + 1;
	$Query .= " LIMIT $Offset, 20";
else:
	$Query .= " LIMIT 0, 20";
endif;

I see what it's doing - for Page 2 it is giving LIMIT 41, 20 and worked it out so that it works and gives the proper LIMIT 21, 20 but it seems cumbersome so I'm wondering if there is a better way:

Code:
if ($_GET['Page']) :
	$Offset = (20 * $_GET['Page']) - 19;
	$Query .= " LIMIT $Offset, 20";
else:
	$Query .= " LIMIT 0, 20";
endif;
 
for twenty results per page your $offset should be

PHP:
20 * ($page -1)

Otherwise page 2 will start at row 40, 3 at row 60 and so on


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Thank you! It seems to work as well as my $Offset = (20 * $_GET['Page']) - 19; did but yours makes more sense and looks cleaner.
 
in case you ever find it useful there is a data paging FAQ with sample code written by sleipnir214 in the php forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top