English 中文(简体)
用于排序的 PHP 移动移动挂接
原标题:PHP-Modifing Pagination for Sorting

嗨,我是新程序员

i have a simple pagination which works fine, i have sorting options for sorting my recordset result by id, title etc which is also working fine, the codes are below.

我现在要“强”和“强”两个词都“强”两个词,并具备一个功能,在这两个条件上,方格应该起作用。

that is when recordset result is displayed by default pagination should work as before. and when recordset result is sorted by options pagination should work on sorted result too.

我发现了一些东西,代码在下面, 但我不能让它工作。

我的基本记录和分级代码是:

<?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

我用于排序选项的代码 :

         Sort by
        <a href="?orderBy=id">id:</a>        

        <a href="?orderBy=title">title:</a>

我的射程代码是:

if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo " <li><a href= $pagename?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?page=$x >$x</a></li>";
} else {
echo " <li><a href= $pagename?page=$x >$x</a></li> ";
}}}  
}

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

upto now everytthing is working except pagination only paginate defaut recordset resunt not sorted recordset result for that i have to change url parameter for pagination to work on sorting result, so i have changed my pagination links as

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> ";
} 

i.e added orderBy=$order in links for pagination, but pagination is not working now, not on default recordset result and not on sorted recordset result.

请你看看我做错了什么

最佳回答

我没有读过整个代码,但您在查询字符串中至少有一个错误。 每一个 Get 参数应该用“ amp;” 而不是“ ” 来分隔, 您所做的是“ & amp;” 。 更改您的代码

 ?orderBy=$order,page=$nextpage  

 ?orderBy=$order&page=$nextpage  

至少应该修正此错误 。

You should put error_reporting(E_ALL); at the beginning of your code 至see notices from php (which would have helped you a lot). If it s still not working afterwards you should debug your GET params with

<?php var_dump($_GET); ?>
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签