Skip site navigation (1) Skip section navigation (2)

Paginating results

From: Ângelo Marcos Rigo <angelo_rigo(at)yahoo(dot)com(dot)br>
To: pgsql-php(at)postgresql(dot)org
Subject: Paginating results
Date: 2003-07-18 14:34:05
Message-ID: 20030718143405.32341.qmail@web14802.mail.yahoo.com (view raw or flat)
Thread:
Lists: pgsql-php
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(at)yahoo(dot)com(dot)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/

pgsql-php by date

Next:From: Rod TaylorDate: 2003-07-19 02:16:25
Subject: Re: postgreSQL web form; Security
Previous:From: Frank FinnerDate: 2003-07-17 19:14:24
Subject: Fw: Re: Vexing PHP problem - browser hangs.

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group