pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Lynna Landstreet <lynna@gallery44.org>
To: Ângelo Marcos Rigo <angelo_rigo@yahoo.com.br>
To: pgsql-php@postgresql.org
Subject: Re: Paging results
Date: Wed, 13 Aug 2003 19:12:43 -0400
Message-ID: <BB603E2B.98C%lynna@gallery44.org> (raw)
In-Reply-To: <20030811173147.77918.qmail@web14808.mail.yahoo.com>

on 8/11/03 1:31 PM, Ângelo Marcos Rigo at angelo_rigo@yahoo.com.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




view thread (10+ messages)  latest in thread

Message-ID: <BB603E2B.98C%lynna@gallery44.org>
Permalink:  ../BB603E2B.98C%25lynna@gallery44.org/
Also on:    postgresql.org/message-id/BB603E2B.98C%lynna@gallery44.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: lynna@gallery44.org, angelo_rigo@yahoo.com.br
  Subject: Re: Paging results
  In-Reply-To: <BB603E2B.98C%lynna@gallery44.org>
  References: <20030811173147.77918.qmail@web14808.mail.yahoo.com> <BB603E2B.98C%lynna@gallery44.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox