Thread: Paginating results

Paginating results

From
Ângelo Marcos Rigo
Date:
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> ";

    }

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

        }
    }

    // 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/