2016-08-24 103 views
-1

如何为这个脚本创建分页:如何创建分页?

if(isset($_GET['cat'])){ 

    $cat_id=$_GET['cat']; 
    $get_cat_pro = "select * from products where cat_id='$cat_id'"; 

    $run_cat_pro = mysqli_query($db, $get_cat_pro); 
    $count=mysqli_num_rows($run_cat_pro); 

    if($count==0){ 
    echo "<h2>No Product Found in This Categroies!</h2>"; 
    } 

    while ($row_cat_pro=mysqli_fetch_array($run_cat_pro)){ 

    $pro_id = $row_cat_pro['product_id']; 
    $pro_title = substr($row_cat_pro['product_title'],0,25); 

    $pro_cat = $row_cat_pro['cat_id']; 
    $pro_brand = $row_cat_pro['brand_id']; 
    $pro_desc = $row_cat_pro['product_desc']; 

    $pro_image = $row_cat_pro['product_img1']; 

    echo " 
     <div id='single_product' style='margin:0.5%; width:242px; height:470px; float:left; border:5px solid white; box-shadow:0px 0px 10px #000;' class='bg-warning'> 

     <a href='details.php?pro_id=$pro_id' style='text-decoration:none;'> 
      <h5 style='margin-left:10%; text-decoration:none;'><b>$pro_title...</b></h5> 
     </a> 

     <p>  
      <a href='details.php?pro_id=$pro_id' style='text-decoration:none;'> 
      <img src='products_images/$pro_image' style='border:5px solid white;' width='210' height='210' vspace='10' hspace='10'/> 
      </a> 

      <br><br><b style='margin-left:10%;'>Views:</b> &nbsp &nbsp 
      <span class='badge'>15000</span> 
      <br><b style='margin-left:10%;'>Downloads:</b> &nbsp 
      <span class='badge'>100</span> 
     </p> 

     </div> 

    "; 

    } 
} 
+0

了解堆栈溢出,格式化,并且不要发布任何你试过:) –

+0

读取HTTP答案: //stackoverflow.com/questions/2616697/php-mysql-pagination&http://stackoverflow.com/questions/3705318/simple-php-pagination-script&http://stackoverflow.com/questions/22352143/pagination- with-php-and-mysql –

+0

**您的查询不安全。**考虑绑定参数。 –

回答

0

首先,你需要“LIMIT N,M”添加到您的查询,其中N代表偏移和M的结果的量。

接下来你需要做一些数学。

例如,如果你想有每页6个结果

你的极限是这样的LIMIT 0,6。但只在第一页上。 所以现在,你需要一个Get参数来保存当前页面。

所以你可以乘以当前页面。

例如0,6 IST的第一页,第二页需要开始在7和13末等

+0

请参阅上面的脚本。 –