pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ângelo Marcos Rigo <angelo_rigo@yahoo.com.br>
To: pgsql-php@postgresql.org
Subject: Paginating results
Date: Fri, 18 Jul 2003 11:34:05 -0300 (ART)
Message-ID: <20030718143405.32341.qmail@web14802.mail.yahoo.com> (raw)

Hi 

I am trying to write a script to paginate results
the problem is that this script just return something
after the second page the first do not return nothing.
The other problem is that the script just add the next
set of results at the end of the old set of results,
instead of paginate to them.

<?php 
	// Database Connection 
	include 'db.php'; 

	// If current page number, use it 
	// if not, set one! 
	if(!isset($_GET['page'])){ 
	    $page = 1; 
	} else { 
	    $page = $_GET['page']; 
	} 

	// Define the number of results per page 
	$max_results = 10; 

	// Figure out the limit for the query based 
	// on the current page number. 
	$from = (($page * $max_results) - $max_results); 

	// Perform SQL query on only the current page
number's results 
	$sql = pg_query("SELECT * FROM paginar LIMIT $from
OFFSET $max_results"); //LIMIT $pagesize OFFSET
$offset

	while($row = pg_fetch_array($sql)){ 
	    // Build your formatted results here. 
	    echo $row['title']."<br />"; 
	} 

	// Figure out the total number of results in DB: 
	$total_results = pg_result(pg_query("SELECT COUNT(*)
as Num FROM paginar"),0); 

	// Figure out the total number of pages. Always round
up using ceil() 
	$total_pages = ceil($total_results / $max_results); 

	// Build Page Number Hyperlinks 
	echo "<center>Selecione uma página<br />"; 

	// Build Previous Link 
	if($page > 1){ 
	    $prev = ($page - 1); 
	    echo "<a
href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a>&nbsp;";

	} 

	for($i = 1; $i <= $total_pages; $i++){ 
	    if(($page) == $i){ 
		echo "$i&nbsp;"; 
		} else { 
		    echo "<a
href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a>&nbsp;";

	    } 
	} 

	// Build Next Link 
	if($page < $total_pages){ 
	    $next = ($page + 1); 
	    echo "<a
href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";

	} 
	echo "</center>"; 
?> 

=====
Ângelo Marcos Rigo
AMR Informática 
(51) 3348 0870 
Rua Pe. Alois Kades 400/210 
Porto Alegre /RS/Brasil
http://amr.freezope.org
angelo_rigo@yahoo.com.br
 


_______________________________________________________________________
Yahoo! Mail
Mais espaço, mais segurança e gratuito: caixa postal de 6MB, antivírus, proteção contra spam.
http://br.mail.yahoo.com/



Message-ID: <20030718143405.32341.qmail@web14802.mail.yahoo.com>
Permalink:  ../20030718143405.32341.qmail@web14802.mail.yahoo.com/
Also on:    postgresql.org/message-id/20030718143405.32341.qmail@web14802.mail.yahoo.com

 · 

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: angelo_rigo@yahoo.com.br
  Subject: Re: Paginating results
  In-Reply-To: <20030718143405.32341.qmail@web14802.mail.yahoo.com>
  References: <20030718143405.32341.qmail@web14802.mail.yahoo.com>

* 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