对搜索结果进行分页

时间:2015-01-31 12:03:38

标签: php pagination

我正在使用我的脚本分页。但即使是搜索自助餐也很少。由于搜索标准,我的分页也无法正常工作。我希望首先显示所有记录,然后如果根据它显示给定的搜索条件。然后只有我的分页工作。

但我没有得到怎么做

这是我的脚本,用于显示来自数据库的记录

if(isset($_GET['page'])){ $page=$_GET['page']; } else { $page=1; }
$start_page=($page-1)*5;

`$sql="query";

if(isset($_POST['submit']))
{


    $company = $_POST['company'];
    $product = $_POST['product'];


if(isset($_POST['submit']) && ($_POST['company']!='') && ($_POST['product']!=''))
{
$sql= "My query goes here";
}

elseif(isset($_POST['submit']) && ($_POST['company']!=''))
{
$sql= "My query goes here";
}   
elseif(isset($_POST['submit']) && ($_POST['product']!=''))
{
$sql= "My query goes here";
}   

elseif(isset($_POST['submit']))
{
$sql= "My query goes here";

}
}
$sql1 = mysql_query($sql) or die(mysql_error());
//Display
?>
<div class="w-box w-box-blue">
<div class="w-box-header">
   </div>
<div class="w-box-content"> 

    <table id="dt_hScroll" class="table table-striped">
                        <thead><tr>
                        <th>Order ID</th>
                        <th>Company</th>
                        <th>Reference</th>
                        <th>Total Value</th>
                        <th>Action</th>
                        </tr>
                        </thead>
                        <?php
                        while($row=mysql_fetch_array($sql1))
{

    //Display
}
    ?>
    </table>
</div>
</div>
 <?php
 //PAGINATION SCRIPT

$sql1="select COUNT(order_id) from orders";
$rs_result = mysql_query($sql1);

$row1 = mysql_fetch_row($rs_result);
$total_records = $row1[0];
$total_pages = ceil($total_records / 5);

  if ($page > $total_pages) {
   // set current page to last page
   $page = $total_pages;
} // end if
// if current page is less than first page...
if ($page < 1) {
   // set current page to first page
   $page = 1;
} // end if
$range=4;
            echo "<div style='float:left; width:200px;text-align:center; margin-left:40%;'>";
           // echo "<a href='view_student.php?page=".$i."'>".$i."</a> ";

            if ($page > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?page=1'>First</a> ";
   // get previous page num
   $prevpage = $page - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?page=$prevpage'>Previous</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($page - $range); $x < (($page + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $total_pages)) {
      // if we're on current page...
      if ($x == $page) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a> ";
      } // end else

   } // end if
} // end for

// if not on last page, show forward and last page links        
if ($page != $total_pages) {
   // get next page
   $nextpage = $page + 1;
    // echo forward link for next page
   echo " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>Next</a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?page=$total_pages'>Last</a> ";
} // end if

            echo "</div>";

?>  

1 个答案:

答案 0 :(得分:0)

尝试使用某种查询构建器,这将使您的任务更容易。 所有流行的框架都有这种工具。为分页构建 count 查询要容易得多。 您可以在此处查看http://framework.zend.com/manual/1.12/en/zend.db.select.html