2011-08-01 29 views
3

如何将搜索结果分成页面? (如第1页,第2页,第3页...)如何将搜索结果分成页面?

当用户在我的电子商务网站上搜索产品时,我希望将结果拆分为多个页面,每页显示大约20个产品。搜索结果是数据库查询的结果。

例如:如果三星手机用户搜索,所以我的查询将是:

SELECT * FROM PRODUCTS WHERE BRAND='SAMSUNG';

假设上述查询返回结果55,如何向他们展示到页面(1,2和3)?

我在Windows机器上使用PHP,MySQL,Apache

+1

请参阅http://stackoverflow.com/questions/6793105/how-to-write-php-code-to-go-through-records-一在-A-时间/ 679 3152#6793152 – Rasel

回答

11

适当的SQL将被添加:

LIMIT start, amount 

可以导航像

search.php?start=20 

,然后这样的代码:

LIMIT $start, $amount 

$start = intval($_GET['start']); 

$amount = 20; 

,这将导致最多20个记录的页面。

+0

好的。我会做的。但是,我怎么知道有多少页?我应该首先查询并计算没有LIMIT的行数并除以20吗?然后显示页数后,我可以在不同的页面上显示结果。如果我错了,请纠正。 –

+0

SELECT SQL_CALC_FOUND_ROWS ... LIMIT 10,10。下一个查询:select found_rows() – RiaD

+2

@iSumitG:我必须说我不是一个专业的编码器,但我会做的是执行一个查询,如'SELECT * FROM table',然后使用'ceil(mysql_num_rows()/ 20 )'。至于当前页面,你可以使用'$ page = floor($ start/20)+ 1'(因为它将从'0开始)。 – pimvdb

3

使用SQL的LIMIT关键字可以限制查询结果的数量;例如:

SELECT * FROM BRAND ='SAMSUNG'LIMIT 20,40;

这将选择20个元素,开始在第40

+0

这应该这样做,更多信息尝试谷歌'分页' – Johan

0

是的,你可以运行一个查询使用限制

例如用来获得总记录数,比使用查询: SELECT COUNT(ID)从table_name的

这将返回数据库记录总数

2

下面是完整的代码:

<?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); 
} 
?> 

希望它的帮助。

0

在我的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(); 
?> 

希望它有助于解决方案,刚刚尝试了几个小时前我的搜索脚本页面(从书籍中学习)