Hi i am a new programmer.
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.
now i want to combine both and have a functionality that pagination should work on both conditions.
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.
i got something figured out, the codes are below, but i cant get it to work.
my code for recordset and basic pagination-sorting are:
<?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
my codes for sorting options:
Sort by
<a href="?orderBy=id">id:</a>
<a href="?orderBy=title">title:</a>
my codes for pagination are:
if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo " <li><a href='$pagename?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?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'>««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»»</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> ";
}
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.
please see what i am doing wrong
I didn’t read the whole code but you have at least one error in your query string. Each GET-parameter should be separated by “&” instead of “,” what you did. Changing your code from
to
should fix this error at least.
You should put error_reporting(E_ALL); at the beginning of your code to 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