我如何使用 PHP 来 limit/paginate 我的 MongoDB 搜索结果?
How do I use PHP to limit/paginate my MongoDB search results?
我想按照 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>
我想按照 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>