Home » Php » Pagination trouble with php and postgresql

Pagination trouble with php and postgresql

Posted by: admin February 23, 2020 Leave a comment

Questions:

I am trying to do a simple pagination exercise with HTML table but only page 2 shows info. It doesn’t show table information when I hit ‘previous’ button.

For example, when I open the page, it shows nothing. When I click on page 2, there is information. When I reach the end, all is ok but then I click ‘previous’ button or page 1 and it does not show anything again.

$rowperpage = 5; // Total rows display
 $row = 0;


 if(isset($_GET['page'])){
  $row = $_GET['page']-1;
  if($row < 0){
   $row = 0;
  }
 }

$link = pg_connect("host=127.0.0.1 port=5432 dbname=swxxxg5_en user=swapng5_control password=xxxxxxxx");
$query = "select id from registrados";
$resultfichados = pg_query($link, $query);
$totalfichados = pg_num_rows($resultfichados);
$allcount = pg_num_rows($resultfichados);
//echo $allcount;
$limitrow = $row*$rowperpage;


$db = pg_connect("host=127.0.0.1 port=5432 dbname=swxxxg5_en user=swapng5_control password=xxxxxxxxxxxxx");
$result = pg_query($db,"select
id,
usuario,
nombre,
apellido,
flag,
reputacion,
ingreso,
lastlogin,
passport,
loyalty,
certified
from registrados order by id asc limit " . $limitrow . " offset 0");
//from registrados order by reputacion asc limit 5 offset 0");




?>
<table align='center' class='table table-hover table-striped' id='t01'>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>id</td>    
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Usuario</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Nombre</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Apellido</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Flag</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Reputacion</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Ingreso</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Lastlogin</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Passport</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Loyalty</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Certified</td>
<?php
while($rowdatauser=pg_fetch_assoc($result)){
echo "<tr>";
echo "<td align='center' width='200' style='color:#7f8c8d; font-weight:bold;'>" . $rowdatauser['id'] . "</td>";
echo "<td align='center' width='200' style='color:#7f8c8d; font-weight:bold;'>" . $rowdatauser['usuario'] . "</td>";
echo "<td align='center' width='200'>" . $rowdatauser['nombre'] . "</td>";
echo "<td align='center' width='60'>" .  $rowdatauser['apellido'] . "</td>";
echo "<td align='center' width='60'  style='background: #e74c3c; color:#fff; font-weight:bold;'>" .  $rowdatauser['flag'] . "</td>";
echo "<td align='center' width='200' style='background: #ff9900; font-weight:bold;'>" . $rowdatauser['reputacion'] . "</td>";
echo "<td align='center' width='200'>" . $rowdatauser['ingreso'] . "</td>";
echo "<td align='center' width='200' style='background: #58c0ce;'>" . $rowdatauser['lastlogin'] . "</td>";
echo "<td align='center' width='200'>" . $rowdatauser['passport'] . "</td>";
echo "<td align='center' width='200'style='background: #7863a0; color:white; font-weight:bold;'>" . $rowdatauser['loyalty'] . "</td>";
echo "<td align='center' width='200'>" . $rowdatauser['certified'] . "</td>";
echo "</tr>";
}
echo "   <td align='center' style='background: #fff;'></td>";
echo "   <td align='center' style='background: #fff;'></td>";
echo "   <td align='center' style='background: #fff;'></td>";
echo "   <td align='center' style='background: #fff;'></td>";
echo "   <td align='center' style='background: #fff;'></td>";
echo "   <td align='center' style='background: #fff;'></td>";
echo "   <td align='center' style='background: #fff;'></td>";
echo "   <td align='center' style='background: #fff;'></td>";
echo "   <td align='center' style='background: #fff;'></td>";
echo "   <td align='center' style='background: #4567;'>TOTALES</td>";
echo "<td align='center' width='200' style='background: #eee;'>", $totalfichados ,"</td>";
echo "</table>";

?>

 <!-- Number list (start) -->
 <ul class="pagination">

 <?php
 // calculate total pages
 $total_pages = ceil($allcount / $rowperpage);

 $i = 1;$prev = 0;

 // Total number list show
 $numpages = 5;

 // Set previous page number and start page 
 if(isset($_GET['next'])){
  $i = $_GET['next']+1;
  $prev = $_GET['next'] - ($numpages);
 }

 if($prev <= 0) $prev = 1;
 if($i == 0) $i=1;

 // Previous button next page number

 $prevnext = 0;
 if(isset($_GET['next'])){
  $prevnext = ($_GET['next'])-($numpages+1);
  if($prevnext < 0){
   $prevnext = 0;
  }
 }

 // Previous Button
 echo '<li ><a href="?page='.$prev.'&next='.$prevnext.'">Previous</a></li>';

 if($i != 1){
  echo '<li ><a href="?page='.($i-1).'&next='.$_GET['next'].'" '; 
  if( ($i-1) == $_GET['page'] ){
   echo ' class="active" ';
  }
  echo ' >'.($i-1).'</a></li>';
 }

 // Number List
 for ($shownum = 0; $i<=$total_pages; $i++,$shownum++) {
  if($i%($numpages+1) == 0){
   break;
  }

  if(isset($_GET['next'])){ 
   echo "<li><a href='?page=".$i."&next=".$_GET['next']."'";
  }else{
   echo "<li><a href='?page=".$i."'";
  }

  // Active
  if(isset($_GET['page'])){
   if ($i==$_GET['page']) 
    echo " class='active'";
   }
   echo ">".$i."</a></li> ";
  }

  // Set next button
  $next = $i+$rowperpage;
  if(($next*$rowperpage) > $allcount){
   $next = ($next-$rowperpage)*$rowperpage;
  }

  // Next Button
  if( ($next-$rowperpage) < $allcount ){ 
   if($shownum == ($numpages)){
    echo '<li ><a href="?page='.$i.'&next='.$i.'">Next</a></li>';
   }
  }

  ?>
 </ul>
 <!-- Numbered List (end) -->
How to&Answers:

I guess you are trying to paginaate using LIMIT a OFFSET b system. In that case, your a should be the page size and b the place where you start.
This system has it’s limitations, like when rows are added or removed the same time you are viewing data, but if this is acceptable, then it might be the easiest way.

Other ways to paginate are shown in this article.

Two notes: One DB handle is enough, you don’t need two connections. And if you want to count the rows in yous set, SELECT count(*) FROM... is easier than pg_num_rows()