如何將搜索結果分成頁面? (如第1頁,第2頁,第3頁...)如何將搜索結果分成頁面?
當用戶在我的電子商務網站上搜索產品時,我希望將結果拆分爲多個頁面,每頁顯示大約20個產品。搜索結果是數據庫查詢的結果。
例如:如果三星手機用戶搜索,所以我的查詢將是:
SELECT * FROM PRODUCTS WHERE BRAND='SAMSUNG';
假設上述查詢返回結果55,如何向他們展示到頁面(1,2和3)?
我在Windows機器上使用PHP
,MySQL
,Apache
。
如何將搜索結果分成頁面? (如第1頁,第2頁,第3頁...)如何將搜索結果分成頁面?
當用戶在我的電子商務網站上搜索產品時,我希望將結果拆分爲多個頁面,每頁顯示大約20個產品。搜索結果是數據庫查詢的結果。
例如:如果三星手機用戶搜索,所以我的查詢將是:
SELECT * FROM PRODUCTS WHERE BRAND='SAMSUNG';
假設上述查詢返回結果55,如何向他們展示到頁面(1,2和3)?
我在Windows機器上使用PHP
,MySQL
,Apache
。
適當的SQL將被添加:
LIMIT start, amount
可以導航像
search.php?start=20
,然後這樣的代碼:
LIMIT $start, $amount
與
$start = intval($_GET['start']);
和
$amount = 20;
,這將導致最多20個記錄的頁面。
好的。我會做的。但是,我怎麼知道有多少頁?我應該首先查詢並計算沒有LIMIT的行數併除以20嗎?然後顯示頁數後,我可以在不同的頁面上顯示結果。如果我錯了,請糾正。 –
SELECT SQL_CALC_FOUND_ROWS ... LIMIT 10,10。下一個查詢:select found_rows() – RiaD
@iSumitG:我必須說我不是一個專業的編碼器,但我會做的是執行一個查詢,如'SELECT * FROM table',然後使用'ceil(mysql_num_rows()/ 20 )'。至於當前頁面,你可以使用'$ page = floor($ start/20)+ 1'(因爲它將從'0開始)。 – pimvdb
使用SQL的LIMIT
關鍵字可以限制查詢結果的數量;例如:
SELECT * FROM BRAND ='SAMSUNG'LIMIT 20,40;
這將選擇20個元素,開始在第40
這應該這樣做,更多信息嘗試谷歌'分頁' – Johan
是的,你可以運行一個查詢使用限制
例如用來獲得總記錄數,比使用查詢: SELECT COUNT(ID)從table_name的
這將返回數據庫記錄總數
下面是完整的代碼:
<?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);
}
?>
希望它的幫助。
在我的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";
}
}
}
}
}
?>
然後我們把它放在需要分頁腳本
<!-- 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();
?>
希望它有助於解決方案,剛剛嘗試了幾個小時前我的搜索腳本頁面(從書籍中學習)
請參閱http://stackoverflow.com/questions/6793105/how-to-write-php-code-to-go-through-records-一在-A-時間/ 679 3152#6793152 – Rasel