Javascript 如何将搜索结果分页?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6896412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to split search results into pages?
提问by Sumit Gupta
How to split the search results into pages? (like page 1, page 2, page 3...)
如何将搜索结果分页?(如第 1 页、第 2 页、第 3 页...)
When the user searches for products on my e-commerce website, I want results to be split into several pages showing around 20 products per page. The search results are the outcome of database query.
当用户在我的电子商务网站上搜索产品时,我希望将结果分成几个页面,每页显示大约 20 个产品。搜索结果是数据库查询的结果。
For example:If the user searches for Samsung mobiles so my query will be:
例如:如果用户搜索三星手机,那么我的查询将是:
SELECT * FROM PRODUCTS WHERE BRAND='SAMSUNG';
SELECT * FROM PRODUCTS WHERE BRAND='SAMSUNG';
Suppose the above query returns 55 results, how to show them into pages (1,2 and 3)?
假设上面的查询返回55个结果,如何将它们显示到页面(1,2和3)中?
I am using PHP
, MySQL
, Apache
on Windows machine.
我使用的PHP
,MySQL
,Apache
Windows机器上。
回答by pimvdb
The appropriate SQL would be adding:
适当的 SQL 将添加:
LIMIT start, amount
You can navigate like
你可以像导航一样
search.php?start=20
and then code like:
然后代码如下:
LIMIT $start, $amount
with
和
$start = intval($_GET['start']);
and
和
$amount = 20;
That will result in max 20 records a page.
这将导致每页最多 20 条记录。
回答by TJHeuvel
Use SQL's LIMIT
keyword to limit the amount of results from your query; for example:
使用 SQL 的LIMIT
关键字来限制查询结果的数量;例如:
SELECT * FROM PRODUCTS WHERE BRAND='SAMSUNG' LIMIT 20, 40;
SELECT * FROM PRODUCTS WHERE BRAND='SAMSUNG' LIMIT 20, 40;
This would select 20 elements, starting at the 40th
这将选择 20 个元素,从第 40 个开始
回答by Anthony Simmon
Here is the complete code:
这是完整的代码:
<?php
// Requested page
$requested_page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Get the product count
$r = mysql_query("SELECT COUNT(*) FROM PRODUCTS WHERE BRAND='SAMSUNG'");
$d = mysql_fetch_row($r);
$product_count = $d[0];
$products_per_page = 20;
// 55 products => $page_count = 3
$page_count = ceil($product_count / $products_per_page);
// You can check if $requested_page is > to $page_count OR < 1,
// and redirect to the page one.
$first_product_shown = ($requested_page - 1) * $products_per_page;
// Ok, we write the page links
echo '<p>';
for($i=1; $i<=$page_count; $i++) {
if($i == $requested_page) {
echo $i;
} else {
echo '<a href="/products/samsung/'.$i.'">'.$i.'</a> ';
}
}
echo '</p>';
// Then we retrieve the data for this requested page
$r = mysql_query("SELECT * FROM PRODUCTS WHERE BRAND='SAMSUNG' LIMIT $first_product_shown, $products_per_page");
while($d = mysql_fetch_assoc($r)) {
var_dump($d);
}
?>
Hope its help.
希望它的帮助。
回答by skeith
In my php learning books, it provides a solution using a PHP class it looks like this
在我的 php 学习书籍中,它提供了一个使用 PHP 类的解决方案,它看起来像这样
<!-- language: php -->
<?php
error_reporting(0); // disable the annoying error report
class page_class
{
// Properties
var $current_page;
var $amount_of_data;
var $page_total;
var $row_per_page;
// Constructor
function page_class($rows_per_page)
{
$this->row_per_page = $rows_per_page;
$this->current_page = $_GET['page'];
if (empty($this->current_page))
$this->current_page = 1;
}
function specify_row_counts($amount)
{
$this->amount_of_data = $amount;
$this->page_total=
ceil($amount / $this->row_per_page);
}
function get_starting_record()
{
$starting_record = ($this->current_page - 1) *
$this->row_per_page;
return $starting_record;
}
function show_pages_link()
{
if ($this->page_total > 1)
{
print("<center><div class=\"notice\"><span class=\"note\">Halaman: ");
for ($hal = 1; $hal <= $this->page_total; $hal++)
{
if ($hal == $this->current_page)
echo "$hal | ";
else
{
$script_name = $_SERVER['PHP_SELF'];
echo "<a href=\"$script_name?page=$hal\">$hal</a> |\n";
}
}
}
}
}
?>
then we call it on the script that require paging
然后我们在需要分页的脚本上调用它
<!-- language: php -->
<?php $per_page = 5;
$page = new Page_class($per_page);
error_reporting(0); // disable the annoying error report
$sql="SELECT * FROM table WHERE condition GROUP BY group";
$result=mysql_query($sql) or die('error'.mysql_error());
// paging start
$row_counts = mysql_num_rows($result);
$page->specify_row_counts($row_counts);
$starting_record = $page->get_starting_record();
$sql="SELECT * FROM table WHERE condition GROUP BY group LIMIT $starting_record, $per_page";
$result=mysql_query($sql) or die('error'.mysql_error());
$number = $starting_record; //numbering
$num_rows = mysql_num_rows($result);
if ($num_rows == 0 )
{ // if no result is found
echo "<div class=\"notice\">
<center><span class=note>NO DATA</span></center>
</div>";
}
else {
// while goes here ...
}
?>
// call the page link
<?php
$page->show_pages_link();
?>
hope it helps, just tried it hours ago to my search script page (learning from books)
希望它有帮助,几个小时前刚刚在我的搜索脚本页面尝试过(从书本中学习)
回答by Sumair Zafar
Yes you can run a query to get total record count and than use query using limit
是的,您可以运行查询来获取总记录数,而不是使用限制来使用查询
exampe: select count(id) from table_name
示例:从 table_name 中选择 count(id)
This will return total record count in database
这将返回数据库中的总记录数