Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7985803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:39:49+00:00 2026-06-04T11:39:49+00:00

Hi i am a new programmer. i have a simple pagination which works fine

  • 0

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&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.

please see what i am doing wrong

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:39:51+00:00Added an answer on June 4, 2026 at 11:39 am

    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

    '?orderBy=$order,page=$nextpage' 
    

    to

    '?orderBy=$order&page=$nextpage' 
    

    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

    <?php var_dump($_GET); ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a rather new C++ programmer. I have made a very simple game
I am a very new programmer, I have made a couple basic applications, however
I am a relatively new programmer so this may be a really simple question
New Rails programmer here. There is probably a pretty simple solution to this, but
I'm a new Java programmer, and I've been trying to setup a simple Swing
New Programmer here. Trying space invaders. I have a 2 dimensional array of objects
I'm new here, also a novice programmer, and not really familiar with PHP. I
I have programmed a simple hibernate app which isn't updating the table. The following
I have programmed a simple hibernate app which leaves me with errors. The following
I'm new to OpenGL. I have written a programm before getting to know OpenGL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.