如何使用PHP限制/分页我的MongoDB搜索结果?

问题描述

我想按照Google的方式将搜索结果分为页面。我试过以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>

当我打开网页时,将显示五个结果(按预期),但是底部的按钮不会将我定向到任何其他页面。我在做什么错了?

我从本sql教程中获得了大部分代码方法是: https://www.youtube.com/watch?v=takddjxhWT0

解决方法

我知道了! @AminShojai是正确的。

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