分页如何与排序和搜索功能一起工作

时间:2014-04-20 17:43:03

标签: php sorting search pagination

我在分页,搜索和排序功能方面遇到问题。 如果dint将所有内容与分页结合起来,它的功能很好。一旦我排序,结果在每个页面都没有改变,这意味着只会在当前页面中改变。例如,现在我在第2页,然后我排序,第2页的结果由于我的排序而改变了。但是,其他页面没有改变,它们仍然是默认的。可能我知道出了什么问题。感谢你的帮助。

<?php

include('db_connection.php');

$sql = "SELECT COUNT(*) FROM product";

$r = mysql_fetch_array(mysql_query($sql));
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 2;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;
$sOrder = "ORDER BY  product_id  ASC "; 

if(isset($_POST["submit"]))
{
    $sort = $_POST["sort"];
        if($sort == "latest")
        {
            $sOrder = "ORDER BY date_update DESC "; 
        }
        else if ($sort == "lp")
        {
            $sOrder =  "ORDER BY  pro_price ASC"; 
        }
        else if ($sort == "hp")
        {
           $sOrder = "ORDER BY  pro_price DESC" ; 
        }

        else if ($sort == "AZ")
        {
             $sOrder =  "ORDER BY pro_name ASC";
        }

        else if ($sort == "ZA")
        {
             $sOrder =  "ORDER BY pro_name DESC"; 
        }

        else
        {
             $sOrder = " "; 
        }




if ( isset($_POST['sSearch']) && $_POST['sSearch'] != "" )
{
    $search=mysql_real_escape_string( $_POST['sSearch'] );
    $sWhere = " where pro_name LIKE '%".mysql_real_escape_string(           $_POST['sSearch'] )."%' " ;
    }   
else{
    $sWhere = " ";
}


$currentpage = 1;
}
?>
<html>
<head>
</head>
<body>

<form method = "POST">
 Sort by 
 <select name ="sort">
 <option value="all" checked>All</option>
 <option value="latest">Latest item</option>
<option value="lp">Lowest Price first</option>
<option value="hp">Highest Price first</option>
<option value="AZ">Alphabets A-Z</option>
<option value="ZA">Alphabets Z-A</option>
</select>

<input type="text" name="sSearch" /> 
<input type="submit" name="submit" value="submit">
</form>
<?php

// get the info from the db 
$sql = "SELECT * FROM product $sWhere $sOrder LIMIT $offset, $rowsperpage";
$result = mysql_query($sql) ;
// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
$pid = $list['product_id'];
?>
<div class='grid'>
<ul class="products">
<li class="cart_items">
<a href="proDetails.php?pid=<?php echo $pid ?>" class="product-image">
<img src="<?php echo "product/".$list['pro_photo']; ?>" style="max-height:140px;max-    width:140px" />
<?php echo "<br/>".htmlspecialchars($list['pro_name']); ?>
</a>
<?php echo "<br/> RM ".$list['pro_price']; ?>
&nbsp;
<form name = "btn" method = "POST" action="" >
<input type="hidden" name="product_id" value="<?php echo $pid ?>"/>
<input type="number" name="qty" min="1" max="30" value="1" size="30" />
</br>
<a href="" name="favourite" title="favourite"><img src="images/heart-add-icon.png"       name="favourite" onclick=""/></a>

<input type="submit" name="add" value="Add to Cart" class="add-to-cart" />

</form>
</li>
</ul>

<?php
    }

/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
  // if we're on current page...
  if ($x == $currentpage) {
     // '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']}?currentpage=$x'>$x</a> ";
  } // end else
} // end if 
} // end for

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

1 个答案:

答案 0 :(得分:0)

在第一行中,您将检索所有数据库记录计数。因此,$numrows然后$totalpages始终是错误的,并且等于仅检索的行而不进行任何搜索。

要进行实际计数,您应首先构建搜索查询。 实施例:

$querypart = ' from table where ...... order by ....';
$countQuery = 'select count(*) '.$querypart;
$query = 'select * '.$querypart;

现在,如果您执行$countQuery,您将获得给定搜索和排序的行数。并且为了执行$query,您将获得行。 当然,您应该在$query .= ' limit '.($page*$onPage).','.$onPage';的末尾添加广告,这样您只会检索所需的行页面,而不是每个人。