0

嗨,我有一个工作正常的基于 php 的分页,但是当我通过排序链接对数据进行排序时,分页不起作用..

我使用的排序方法具有与 mysql 查询的直接链接排序。

请看看我做错了什么。

到目前为止我的完整代码

    <?php 
    $table='mytable';
    $pagename = "is-test.php";
$db = mysql_select_db($database,$connection) or trigger_error("SQL", E_USER_ERROR);
$sql1 = "SELECT COUNT(*) FROM $table";
$result1 = mysql_query($sql1, $connection) or trigger_error("SQL", E_USER_ERROR);
$row = mysql_fetch_row($result1);
$numrows = $row[0];
$rowsperpage = 5;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
$currentpage = (int) mysql_real_escape_string($_GET['page']);
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$orderBy = array('id', 'title',);
$order = '';
if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) {
$order = mysql_real_escape_string($_GET['orderBy']);
}else{
$order='id';
}
$offset = ($currentpage - 1) * $rowsperpage;
$sql2 = "SELECT * FROM $table ORDER BY $order ASC LIMIT $offset, $rowsperpage";
$result2 = mysql_query($sql2, $connection) or trigger_error("SQL", E_USER_ERROR);
$list = mysql_fetch_assoc($result2);
$startrow = ($currentpage-1) * $rowsperpage;

////////////////////////LINKS FOR SORTING///////////////////////
         echo "<a href=orderBy=id,>id:</a>";
         echo "<a href=orderBy=title,>title:</a>";
////////////////////////////////////////////////////////////////

//////////////////////////PAGINATION///////////////////////////
echo " <li><a href='$pagename?orderBy=$order,page=$totalpages'>$totalpages</a></li> ";


if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo " <li><a href='$pagename?orderBy=$order,page=$nextpage'>Next&raquo;&raquo;</a></li> ";
}

if($currentpage<$totalpages){
for ($x = ($currentpage - 3); $x < (($currentpage + 3) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " <li id='pcurrent'><a href='$pagename?orderBy=$order,page=$x'>$x</a></li>";
} else {
echo " <li><a href='$pagename?orderBy=$order,page=$x'>$x</a></li> ";
}}}  
}

if ($currentpage > 1){
$prevpage = $currentpage - 1;
echo " <li><a href='$pagename?page=$prevpage'>&laquo;&laquo;Prev</a></li> ";
} 
if ($currentpage > 5) {
echo "<li><a href='$pagename?page=1'>1</a></li> ";
}
    ?>
4

1 回答 1

0

刚刚看到:您的 URL 调用中有逗号而不是 & :

echo " <li><a href='$pagename?orderBy=$order,page=$nextpage'>Next&raquo;&raquo;</a></li> ";

改成:

echo " <li><a href='$pagename?orderBy=$order&page=$nextpage'>Next&raquo;&raquo;</a></li> ";
  • 更改其他有此问题的行
于 2012-05-27T09:09:39.643 回答