views:

39

answers:

2

PHP learner. The pagination script below seems to generally work, but the problem is that on each page of 20 records, when I select some rows (via checkboxes) and click the SUBMIT button, the display of 20 records jumps back to page 1. The rows are selected without a problem, but I can't grasp why it returns to page 1.

Is the problem obvious in the script below? Thanks!

//-----------------|
// Add pagination.
//-----------------|

$nAdjacentPages =  3; 

// If the current page number is greater than 1, then display:
// "<<" and "<" (i.e., << < ).

if ($nCurrentPage > 1) 
   {
      echo " <a href = 
         '{$_SERVER['PHP_SELF']}?nCurrentPage=1'> << </a> " ;
      $nPreviousPage = $nCurrentPage - 1 ;
      echo " <a href = 
         '{$_SERVER['PHP_SELF']}?nCurrentPage=$nPreviousPage'> < </a> ";
   }   

// Appearance of page links when viewing page 5:
//     << <  2  3  4  [5]  6  7  8  > >>  

for ( $x = ( $nCurrentPage - $nAdjacentPages ) ; 
      $x < ( ( $nCurrentPage + $nAdjacentPages ) + 1 ) ; 
      $x++ ) 
   {
      // if it's a valid page number...

      if ( ( $x > 0 ) and ( $x <= $nTotalPages ) ) 
         {
            // If on current page, 'highlight' but do not link.
            // If not current page, make it a link.

            if ( $x == $nCurrentPage ) 
               {
                  echo " [<b> $x </b>] " ;
               } 
            else 
               {
                  echo " <a href=
                     '{$_SERVER['PHP_SELF']}?nCurrentPage=$x'> $x </a> " ;
               } 
         } 
   } 

// If not last page, show '>' and '>>' links.

if ( $nCurrentPage != $nTotalPages ) 
   {
      $nNextPage = $nCurrentPage + 1;
      echo " <a href = 
         '{$_SERVER['PHP_SELF']}?nCurrentPage=$nNextPage'> > </a> ";
      echo " <a href = 
         '{$_SERVER['PHP_SELF']}?nCurrentPage=$nTotalPages'> >> </a> ";
   } 
?>
A: 

The form that includes the submit button and the checkboxes needs to include a hidden input that gives the current value of the nCurrentPage variable.

PaulC
A: 

It's jumping back to page 1 because your form's action is probably just script.php.

If your form uses the GET method, then add a hidden field named nCurrentPage to the form with the correct value of the current page.

If your form uses the POST method, then add ?nCurrentPage=$nCurrentPage (or whatever...) to the form action (to make it, for example, script.php?nCurrentPage=3), or use the hidden field method above and change this script to also check $_POST in addition to $_GET which it seems to already do.

SoapBox
Ok, thanks! It looks like that's going to do it! Thanks for reviewing.
dave