猿问

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

我想像谷歌那样将我的搜索结果分成页面。我尝试以 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>

当我打开网页时,将显示五个结果(按预期),但底部的按钮不会引导我进入任何其他页面。我究竟做错了什么?

拉风的咖菲猫
浏览 77回答 1
1回答

忽然笑

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