亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

PHP
拉風的咖菲貓 2023-09-22 17:53:25
我想像谷歌那樣將我的搜索結果分成頁面。我嘗試以 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>當我打開網頁時,將顯示五個結果(按預期),但底部的按鈕不會引導我進入任何其他頁面。我究竟做錯了什么?
查看完整描述

1 回答

?
忽然笑

TA貢獻1806條經驗 獲得超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' => $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>


查看完整回答
反對 回復 2023-09-22
  • 1 回答
  • 0 關注
  • 99 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號