Pagination in PHP and PostgreSQL

0

Good night, I need a pagination made in PHP and PostgreSQL , I searched the internet but did not find anything 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;"; 
    }
    
asked by anonymous 11.07.2017 / 04:01

1 answer

2

Resolved.

Follow 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 page change:

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