/    Sign up×
Community /Pin to ProfileBookmark

Pagination using MySQL LIMIT clause and PHP

Hi folks,

I’ve got a strange problem where I’m doing some pagination over items in a catalog, and everything seems to work just fine except when I have exactly one more item than I’m showing per page. Meaning that my second page would end up with exactly one item showing on it.

So in this case the problem occurs when I’m showing a category of products that has exactly 21 items. If it had 22 it would seem to work. I’ve checked out other categories of mine that have a last page with grater than 1 but less than 20 items, and they display just fine.

My SQL appears to be correct, because if I run the statement in the MySQL query browser I do indeed just get one record back. Also, if I dump out the number of rows returned by the query I get (int)20 on the first page and (int)1 on the second page, but on the second page my while loop is not triggered. ?

I think this is really a PHP problem (and not MySQL), but I’m hoping someone here could explain to me just what the heck I’m doing wrong.

Here’s my php code:

[code=php]<?
while($thisRow = mysql_fetch_assoc($qryProductSearch)){
$imagename = $thumbnailpath . ‘/’ . substr($thisRow[‘instylenum’],0,strlen($thisRow[‘instylenum’]) – 4) . ‘.jpg’;
if(!file_exists($imagename)){
$imagename = $thumbnailpath . ‘/noimage.jpg’;
}
?>
<div class=”ListItem”>
<table border=0>
<tr>
<td style=”text-align: center;”>
<a href=”products.php?s=<? echo urlencode($thisRow[‘instylenum’]);?>”>
<img src=”<? echo $imagename; ?>” style=”width:100px; height:100px; border:1px solid gray;”>
</a>
</td>
</tr>
<tr>
<td style=”text-align: center;”>
<span class=”redtext bold”><a href=”products.php?s=<? echo urlencode($thisRow[‘instylenum’]);?>” style=”font-size:11px;”><? echo $thisRow[‘instylenum’]; ?></a></span><br>
</td>
</tr>
</table>
</div>
<?}?>
</div>
[/code]

The above while loop just doesn’t seem to want to work when there’s only one row in the data. I’m not sure what I can do. ?

Any help would be greatly appreciated.

Thanks,
Chris

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@ShortsOct 02.2008 — Where is the pagination aspect of it? Mostlikely it is PHP.

My guess would be the start number.

[code=php]
<?php
$page_id = $_GET['page_id']; #page you are on
$limit = 15; #results per page
$start = (($page_id * $limit) - $limit); #where to start the results from
$qryProductSearch = mysql_query("SELECT rows FROM table LIMIT $start,$limit");
while($thisRow = mysql_fetch_assoc($qryProductSearch)){
#do stuff
}
?>
[/code]
Copy linkTweet thisAlerts:
@cjordanauthorOct 02.2008 — I actually figured out my problem yesterday. It was indeed in the PHP. Basically, I needed to be sure that the pointer was set on the first record before beginning the loop.

So now my loop code looks like this:
[code=php]<?
mysql_data_seek($qryProductSearch,0); // make sure we're at the first record in the set...
while($thisRow = mysql_fetch_assoc($qryProductSearch)){
$imagename = $thumbnailpath . '/' . substr($thisRow['instylenum'],0,strlen($thisRow['instylenum']) - 4) . '.jpg';
if(!file_exists($imagename)){
$imagename = $thumbnailpath . '/noimage.jpg';
}
?>
<div class="ListItem">
<table border=0 style="border-collapse:collapse;">
<tr>
<td style="text-align: center;">
<a href="products.php?s=<? echo urlencode($thisRow['instylenum']);?>">
<img src="<? echo $imagename; ?>" style="width:100px; height:100px; border:1px solid gray;">
</a>
</td>
</tr>
<tr>
<td style="text-align: center;">
<span class="redtext bold"><a href="products.php?s=<? echo urlencode($thisRow['instylenum']);?>" style="font-size:11px;"><? echo $thisRow['instylenum']; ?></a></span><br>
</td>
</tr>
</table>
</div>
<?}?>
[/code]



I tried to post to this thread that I'd figured it out, but my posts still go through a moderator, and don't show up in the forum right away. Kind of annoying, but I guess I understand the policy. I'm assuming that once someone's been around the forum for a while, that their posts get pushed out immediately?

Anyway, thanks for responding.
×

Success!

Help @cjordan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...