如何将搜索结果拆分成页面?

时间:2011-08-01 09:20:48

标签: php javascript mysql pagination

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

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

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

SELECT * FROM PRODUCTS WHERE BRAND='SAMSUNG';

假设上面的查询返回55个结果,如何将它们显示到页面(1,2和3)?

我在Windows计算机上使用PHPMySQLApache

5 个答案:

答案 0 :(得分:12)

适当的SQL将添加:

LIMIT start, amount

您可以像

一样导航
search.php?start=20

然后编码如下:

LIMIT $start, $amount

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

$amount = 20;

这将导致每页最多20条记录。

答案 1 :(得分:3)

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

  

SELECT * FROM PRODUCTS BRERE ='SAMSUNG'LIMIT 20,40;

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

答案 2 :(得分: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);
}
?>

希望得到它的帮助。

答案 3 :(得分:0)

是的,您可以运行查询以获取总记录数,而不是使用限制

进行查询

exampe: 从table_name中选择count(id)

这将返回数据库中的总记录数

答案 4 :(得分: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();
?>

希望它有所帮助,只是几小时前尝试过我的搜索脚本页面(从书本中学习)

相关问题