Pagination in PHP and Postgresql

Asked

Viewed 432 times

0

Good evening, I need a pagination made on PHP and Postgresql, I searched the internet but found nothing useful, I tried to apply a PHP/Mysql code but the result was negative.

The code I used:

if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * 5;
$query = pg_query($dbconn,"select * from my table limit 5 offset $start_from") or die(pg_result_error($dbconn));
$total_query = pg_num_rows($query);
$total_pages = ceil($total_query / 5);

Buttons and Links:

for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='index.php?page=".$i."' class=\"textPagina\">".$i."</a>&nbsp;&nbsp;"; 
    }
  • good, if it was negative can also inform what the problem? would be more useful!!!!!

1 answer

2

Solved.

Follows the code:

if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 

$records = 10; // altere aqui o numero de registros por pagina

$start_from = ($page-1) * $records;

$qry = pg_query($dbconn,"select count(*) as total from table"); 
$row_sql = pg_fetch_row($qry); 
$total_records = $row_sql[0]; 
$total_pages = ceil($total_records / $records);

$select = pg_query($dbconn,"select * from table limit $records offset $start_from");

the result of select:

while($row = pg_fetch_assoc($select )){
    echo $row['col1'].' | '.$row['col2'].' | '.$row['col3'].'<br />';
}

links to change pages:

for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='index.php?page=".$i."' class='yourclass'>".$i."</a>&nbsp;&nbsp;"; 
}
  • 3

    If you have resolved you can accept your own answer, Peter.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.