2012-05-27 60 views
0

您好我有一個工作基於PHP分頁工作正常後工作,但是當我排序排序分頁鏈接工作這麼想的數據做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> "; 
} 
    ?> 
+0

您是否收到錯誤消息?你看過日誌嗎?你的桌子上有一個名爲「title」的列嗎? – Filype

回答

1

剛纔看到: 你有逗號,而不是&在您的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> "; 
  • 更改此問題的其他線路
+0

嘗試過..在排序後工作不起作用 –

+0

你有沒有行(數)?最後你有什麼樣的價值?你什麼意思不工作?我用我自己的分頁測試了這個,並且找不到錯誤。 – 2012-05-27 09:24:43

+0

分頁工作時不使用排序(orderBy = $訂單從分頁鏈接中刪除)我得到分頁鏈接值爲page1,page2等,然後如果我排序我的記錄集(orderBy = $訂單添加到分頁鏈接)分頁停止排序的記錄集,我得到的值在分頁鏈接作爲orderBy = id,page1,orderBy = id,page2等,但沒有任何反應 –