simone
simone

Reputation: 1

PHP Pagination Problem

I had search through many websites and tried the different ways provided online, but it cant seen to work. It does not load the information when I click next, last, first, previous. It only loads the first page's result. Please help! Thank you in advance.

function retrieveName($fieldName)
{   
    $i=1;
    if(isset($_GET[$fieldName]))
    {
        mysql_connect("localhost", "root") or die(mysql_error());           
        mysql_select_db("intern") or die(mysql_error()); 


         //This checks to see if there is a page number. If not, it will set it to page 1 

         if (!(isset($pagenum))) 

         { 

         $pagenum = 1; 

         } 

         //Here we count the number of results 

         $intern = $_GET[$fieldName];
         $data = mysql_query("SELECT p.`internName`, p.`internNRIC`, c.`internSchName` FROM `personaldetails` p, `currentinstitution` c WHERE c.`internNRIC`= p.`internNRIC` AND p.`internName` like '%$intern%' || p.`internNRIC` like '%$intern%' || c.`internSchName` like '%$intern%' GROUP BY p.internNRIC") or die(mysql_error()); 

         $rows = mysql_num_rows($data); 

         //This is the number of results displayed per page 

         $page_rows = 1;            


         //This tells us the page number of our last page 

         $last = ceil($rows/$page_rows); 

         //this makes sure the page number isn't below one, or more than our maximum pages 

         if ($pagenum < 1) 

         { 

         $pagenum = 1; 

         } 

         elseif ($pagenum > $last) 

         { 

         $pagenum = $last; 

         } 

         //This sets the range to display in our query 

         $max = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows; 

    PRODUCTION.   //This is your query again, the same one... the only difference is we add $max into it

         $data_p = mysql_query("SELECT p.`internName`, p.`internNRIC`, c.`internSchName` FROM `personaldetails` p, `currentinstitution` c WHERE c.`internNRIC`= p.`internNRIC` AND p.`internName` like '%$intern%' || p.`internNRIC` like '%$intern%' || c.`internSchName` like '%$intern%' GROUP BY p.internNRIC $max ") or die(mysql_error()); 


         //This is where you display your query results

         while($row = mysql_fetch_array( $data_p )) 
         {      
            echo $i. ".";
            echo " NRIC : <a href='InternInfo.php?id='" . $row['internNRIC'] . ">".$row['internNRIC'] ."</a>";
            echo "</br><br/>";
            echo "  Name : ". $row['internName'] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name of School :" . $row['internSchName'];
            echo "</br><br/>";              
            $i++;                       
         } 

         echo "<p>";


         // This shows the user what page they are on, and the total number of pages

         echo " --Page $pagenum of $last-- <p>";


         // First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.

         if ($pagenum == 1) 

         {

         } 

         else 

         {
            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1&searchIntern=$intern'> <<-First</a> ";             
            echo "---Interns Search---";            
            $previous = $pagenum-1;         
            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous&searchIntern=$intern'> <-Previous</a> ";           

         } 

         //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links

         if ($pagenum == $last) 

         {

         } 

         else
         {          
            $next = $pagenum+1;             
            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next&searchIntern=$intern'>Next -></a> ";           
            echo "---Interns Search---";            
            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last&searchIntern=$intern'>Last ->></a> ";

         } 
    }else echo "Please enter your search.";
}   

Upvotes: 0

Views: 412

Answers (2)

Rebecca Scott
Rebecca Scott

Reputation: 2431

As @cwallenpoole says, it looks like $pagenum is scoped outside of the function, and I'm guessing the function is written assuming that register_globals is on, which is generally a very bad thing. I've seen this cause plenty of issues when moving an old (inherited) site to a new server.

To fix that specific problem, replace:

     if (!(isset($pagenum))) 

     { 

     $pagenum = 1; 

     }

with this:

$pagenum = isset($_REQUEST['pagenum']) ?
    (int)$_REQUEST['pagenum'] :
    1;

This sets $pagenum to the request's pagenum value, and defaults to 1 if the page number isn't in the request. It also casts the value to an int which should at least stop one injection attack vector. The rest of the function is another matter...

Upvotes: 0

cwallenpoole
cwallenpoole

Reputation: 82028

Not 100% about this, but it looks like you're using a local variable for $pagenum when you want to use either a parameter (good idea) or a global variable such as $_GET['pagenum']. You're also leaving yourself open to SQL injection. Use mysql_real_escape_string on all variables which need to be used in queries (like $intern).

Upvotes: 1

Related Questions