我有一个工作基于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»»</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'>««Prev</a></li> ";
}
if ($currentpage > 5) {
echo "<li><a href='$pagename?page=1'>1</a></li> ";
}
?>
答案 0 :(得分:1)
刚看到: 你有逗号而不是&amp;在您的网址中:
echo " <li><a href='$pagename?orderBy=$order,page=$nextpage'>Next»»</a></li> ";
更改为:
echo " <li><a href='$pagename?orderBy=$order&page=$nextpage'>Next»»</a></li> ";