Re: Paging results

From: Lynna Landstreet <lynna(at)gallery44(dot)org>
To: =?ISO-8859-1?B?wg==?=ngelo Marcos Rigo <angelo_rigo(at)yahoo(dot)com(dot)br>, <pgsql-php(at)postgresql(dot)org>
Subject: Re: Paging results
Date: 2003-08-13 23:12:43
Message-ID: BB603E2B.98C%lynna@gallery44.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-php

on 8/11/03 1:31 PM, Ângelo Marcos Rigo at angelo_rigo(at)yahoo(dot)com(dot)br wrote:

> I have this script (please adapt the section where the
> results are showed)

Thank you very much - I've implemented this on one of my results pages as a
test, and it seems to be partially working - the results are limited
properly, and there are no errors, but the next and previous links aren't
quite working right.

On the first page, the prev link points to offset 0 and page 1, which I
guess is correct, but the next link leaves offset blank, and has page 0,
instead of having offset 50 (that's what I set $limit to) and page 2 as I
would think it should.

If I enter those values manually in the URL -
http://www.gallery44.org/db/artists_browse_paged.php?offset=50&pgnum=2 - the
second page displays correctly, but again the prev and next links don't work
right. The prev page here shows pgnum=1, which is correct, but it shows
offset as 30 when it should be 50. And the next link shows exactly what it
did on page 1 - offset=&pgnum=0. I've looked at the code, but I don't think
I'm following it well enough to be sure what's going wrong. I've attached a
copy of the file

I kept the code pretty much as it was except for the part where the results
are displayed, and a couple of very minor tweaks (changed the pg_query and
pg_num_rows to pg_exec and pg_numrows because I'm using PHP 4.1, changed the
part where it selects the whole table and uses pg_numrows to count it to a
select count because it seemed more efficient, and changed $conn to the
database connection I'd already opened, $db).

If you or anyone else could suggest what might be going wrong, I'd very much
appreciate it. Here's what I've got:

First part (before displaying query results):

// Paging script - request for parameters.
$offset = $_REQUEST['offset'];
$pgnum = $_REQUEST['pgnum'];

// Make that they are integer - security.
settype($offset, 'integer');
settype($pgnum, 'integer');

// Open database, i.e PostgreSQL
if (!$db) {
echo "An error occured - no database connection exists.\n";
exit;
}

// Initialize variables.
$limit=50; // rows to return
$numresults=pg_exec("SELECT COUNT(*) FROM artists"); //PostgreSQL
// $numrows=pg_numrows($numresults);

// next determine if offset has been passed to script, if not use 0
if (empty($offset)) {
$offset=0;
$pgnum=1;
}

Then comes the display of the results, which is working fine.

Second part:

// calculate number of pages needing links
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a
remainder from division
if ($numrows%$limit) {

// has remainder so add one page
$pages++;
}

echo "\n<p class=\"small\">";
// next we need to do the links to other results
if (pages!=1)
{
if ($pgnum==1) {
print "<a href=\"$PHP_SELF?offset=0&pgnum=1\">PREV</a>
&nbsp; \n";
}
else
{
$prevoffset=$offset-20;
$cpgnum = intval($prevoffset/$limit)+1;
print "<a
href=\"$PHP_SELF?offset=$prevoffset&pgnum=$cpgnum\">PREV</a> &nbsp; \n";
}
}

for ($i=1;$i<=$pages;$i++) { // loop thru
$newoffset=$limit*($i-1);
$cpgnum = $i;
print "<a
href=\"$PHP_SELF?offset=$newoffset&pgnum=$cpgnum\">$cpgnum</a> &nbsp; \n";
}

// check to see if last page
if ($pages!=1)
{
if ($pgnum<$pages) {
$newoffset=$offset+$limit;
$cpgnum = intval(($offset+$limit)/$limit)+1;
print "<a
href=\"$PHP_SELF?offset=$newoffset&pgnum=$cpgnum\">NEXT</a></p>\n";

}
else
{
print "<a
href=\"$PHP_SELF?offset=$newoffset&pgnum=$pages\">NEXT</a></p>\n";

}
}

Many thanks,

Lynna
--
Resource Centre Database Coordinator
Gallery 44
www.gallery44.org

In response to

Browse pgsql-php by date

  From Date Subject
Next Message Ângelo Marcos Rigo 2003-08-14 13:46:46 Update script
Previous Message Lynna Landstreet 2003-08-13 23:08:26 Re: Paging results