【问题标题】:how pagination work with sorting and search function分页如何与排序和搜索功能一起使用
【发布时间】:2014-06-04 19:47:08
【问题描述】:

我在分页、搜索和排序功能方面遇到问题。 如果 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>

【问题讨论】:

  • 先搜索排序,再分页。否则它会发生在你身上发生的事情。并且没有理由在您的脚本中使用$_SERVER['PHP_SELF'],而只需使用&lt;a href='?current...。并且不要忘记为每个 get 参数传递排序和搜索参数,否则它们会丢失(你错过了,首先进行搜索和排序,然后分页并忘记搜索和排序)。

标签: php sorting search pagination


【解决方案1】:

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

要进行真正的计数,您应该首先构建搜索查询。 示例:

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

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

【讨论】:

  • 你的意思是替换 $sql = "SELECT COUNT(*) FROM product "; to $sql = "SELECT COUNT(*) FROM product $sWhere $sOrder"; ,仪式??我试了一下,但没有用。我认为 if(isset($_POST["submit"])) 可能有问题。
  • 另外,分页查询不应该在开始时,而是在你已经知道要搜索什么的时候。至于下一页/上一页的链接 - 您应该向它们添加搜索查询和排序顺序。当您创建它们时,它们只是告诉页面,但根本没有搜索限制。
猜你喜欢
  • 2017-08-10
  • 1970-01-01
  • 2013-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-25
  • 2015-12-30
  • 2021-08-09
相关资源
最近更新 更多