Search code examples
phpmongodbpagination

How do I use PHP to limit/paginate my MongoDB search results?


I would like to divide my search results into pages the way Google does. I tried doing it in groups of 5:

<?php
//connect to Atlas and include Composer files
      require 'dbconnection.php';
      require 'vendor/autoload.php';

$page = $_GET["page"];
if ($page=="" || $page=="1")
{
    $page1=0;
}
else{
    $page1=($page*5)-5;
}
      $options=["limit" => 5,
      "skip" => 0
      ];
      $query= $collection->find([], $options);
?>
<!DOCTYPE html>
<html>
<head>
   <title>Results</title>
</head>
<body>

<table>
   <tr>
                          <th>Film</th>
                          <th>Actor</th>
                          <th>Director</th>
                          <th>Year</th>
                          <th>Genre</th>
   </tr>
<?php

     foreach($query as $value) {
         echo "<tr>";
         echo "<td>".$value->film."</td>";
         echo "<td>".$value->actor."</td>";
         echo "<td>".$value->director."</td>";
         echo "<td>".$value->year."</td>";
         echo "<td>".$value->category."</td>";
         echo "<td>";
         echo "</tr>";
      };
echo '</table>';

$countr=$collection->count($query);
$a=$countr/5;
$a=ceil($a);
echo "<br>"; echo "<br>";
    for($b=1;$b<=$a;$b++)
    {
        
        ?><a href="results.php?page=<?php echo $b; ?>" style="text-decoration:none "><?php echo $b." "; ?></a> <?php
        
    }

   ?>


</body>
</html>

When I open up the webpage, five results will display (as intended), but the buttons on the bottom will not direct me to any other pages. What am I doing wrong?

I got most of the code from this SQL tutorial by the way: https://www.youtube.com/watch?v=takddjxhWT0


Solution

  • I figured it out! @AminShojai was correct.

    <?php
    //connect to Atlas and include Composer files
          require 'dbconnection.php';
          require 'vendor/autoload.php';
    
    $page = $_GET["page"];
    if ($page=="" || $page=="1")
    {
        $page1=0;
    }
    else{
        $page1=($page*5)-5;
    }
          $options=['limit' => 5,
          'skip' => $page1
          ];
          $query= $collection->find([], $options);
    ?>
    <!DOCTYPE html>
    <html>
    <head>
       <title>Results</title>
    </head>
    <body>
    
    <table>
       <tr>
                              <th>Film</th>
                              <th>Actor</th>
                              <th>Director</th>
                              <th>Year</th>
                              <th>Genre</th>
       </tr>
    <?php
    
         foreach($query as $value) {
             echo "<tr>";
             echo "<td>".$value->film."</td>";
             echo "<td>".$value->actor."</td>";
             echo "<td>".$value->director."</td>";
             echo "<td>".$value->year."</td>";
             echo "<td>".$value->category."</td>";
             echo "<td>";
             echo "</tr>";
          };
    echo '</table>';
    
    $countr=$collection->count($query);
    $a=$countr/5;
    $a=ceil($a);
    echo "<br>"; echo "<br>";
        for($b=1;$b<=$a;$b++)
        {
            
            ?><a href="results.php?page=<?php echo $b; ?>" style="text-decoration:none "><?php echo $b." "; ?></a> <?php
            
        }
    
       ?>
    
    
    </body>
    </html>