SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast paramedic's Avatar
    Join Date
    Oct 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems with pagination

    Hi guys

    I am having problems with getting my search results to display using pagination. The first page displays correctly (ie 28 results and the first 5 are shown with 6 pages to view), but when I click on next the variables (ie $town) aren't recognised and the next page displays page 1 of 1 even though it should be 2 of 6.


    Here is the code. I hope you can help as it is driving me up the wall.

    HTML Code:
    $town=$_POST["town"];
    $county=$_POST["county"];
    $type=$_POST["type"];
    $price1=$_POST["price1"];
    $price2=$_POST["price2"];
    $bed=$_POST["bed"];
    
    $result= mysql_query("SELECT * FROM propertydetails WHERE town='$town'AND county='$county'AND type='$type'
    AND(price>='$price1'AND price<='$price2')AND bed>='$bed' ",$link) 
    
    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; 
    } 
    
    $num_rows = mysql_num_rows($result); 
    
    echo "Your search has produced  $num_rows matches\n";
    
     
    
    //This is the number of results displayed per page 
    $page_rows = 5; 
    
    //This tells us the page number of our last page 
    $last = ceil($num_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; 
     
    
    
    $data_p = mysql_query("SELECT * FROM propertydetails $max") or die(mysql_error()); 
    
    //This is where you display your query results
    
    while($info = mysql_fetch_array( $data_p )) { 
    
    *******ALL OF THE RESULTS ARE DISPLAYED HERE*******
    }
    // 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'> <<-First</a> ";
    echo " ";
    $previous = $pagenum-1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-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'>Next -></a> ";
    echo " ";
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
    
    }


    Many thanks in advance for your help

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    There's no POST data on the page after the "next" link is clicked because no form was posted to that page. You have to store that information from the first page somewhere; either in a session or passing the variables page to page through the link.

  3. #3
    SitePoint Enthusiast paramedic's Avatar
    Join Date
    Oct 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.
    Sort of understand what you mean, but how do I do that. I am newish to this and am still finding it a bit confusing to say the least.


    Many thanks in advance

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Create the queries as strings and echo them out to see what is missing ie:
    $q = "SELECT FROM ...";
    echo $q;
    mysql_query($q);

    You should notice what's missing. And what's missing is causing $num_rows = mysql_num_rows($result); to be set to zero, hence no rows will display.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •