Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    cannt get this pagination working

    hi all, i cannot figure out how to get the pagination to work.
    the problem is it display the first page of the search results correctly, with the links at the bottom of the page. but after i click "next", it ask me "please enter a search term". Obviously it cannot pass the submitted value. would you please tell me how?

    here is the code:
    Code:
     
    <?php 
    $link = mysql_connect('localhost', 'root', 'pass') or die(mysql_error());
    
    $db_selected = mysql_select_db('database')or die(mysql_error());
    
    $searchterm = @$_GET['searchterm'];
    if ($searchterm == "")
    {
    echo "<p>Please enter a search term</p>";
    exit;
    }
    
    
    // how many rows to show per page 
    $rowsPerPage = 10; 
    
    // by default we show first page 
    $pageNum = 1; 
    
    // if $_GET['page'] defined, use it as page number 
    if(isset($_GET['page'])) 
    { 
        $pageNum = $_GET['page']; 
    } 
    
    // counting the offset 
    $offset = ($pageNum - 1) * $rowsPerPage; 
    
    $query  = "SELECT * FROM table where title like \"%$searchterm%\" LIMIT $offset, $rowsPerPage"; 
    $result = mysql_query($query) or die('Error, query failed');  
    
    // how many rows we have in database 
    $query   = "SELECT COUNT(id) AS numrows FROM table where title like \"%$searchterm%\""; 
    $results  = mysql_query($query) or die('Error, query failed'); 
    $row     = mysql_fetch_array($results, MYSQL_ASSOC); 
    $numrows = $row['numrows']; 
    
    // how many pages we have when using paging? 
    $maxPage = ceil($numrows/$rowsPerPage); 
    
    // print the link to access each page 
    $self = $_SERVER['PHP_SELF']; 
    for($page = 1; $page <= $maxPage; $page++) 
    { 
        if ($page == $pageNum) 
        { 
            $nav .= " $page ";   // no need to create a link to current page 
        } 
        else 
        { 
            $nav .= " <a href=\"$self?page=$page\">$page</a> "; 
        }         
    } 
    
    // creating previous and next link 
    // plus the link to go straight to 
    // the first and last page 
    
    if ($pageNum > 1) 
    { 
        $page = $pageNum - 1; 
    	$searchterm = @$_GET['searchterm'];
        $prev = " <a href=\"$self. \"?page=\" .$page. \"&searchterm =\" .$searchterm;\">[Prev]</a> "; 
        $first = " <a href=\"$self?page=1\">[First Page]</a> "; 
    }  
    else 
    { 
        $prev  = ''; // we're on page one, don't print previous link 
        $first = ''; // nor the first page link 
    } 
    
    if ($pageNum < $maxPage) 
    { 
        $page = $pageNum + 1; 
        $searchterm = @$_GET['searchterm'];
    	$next = " <a href=\"$self. \"?page=\" .$page. \"$searchterm =\" .$searchterm;\">[Next]</a> 
    
    "; 
    	$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> "; 
    }  
    else 
    { 
        $next = ''; // we're on the last page, don't print next link 
        $last = ''; // nor the last page link 
    } 
    
    echo "<H2>Total titles found:&nbsp" . $numrows . "</H2>";
    
    while($row = mysql_fetch_array($result)) 
    {
    
    echo $row['title'];
    
    echo "<hr>";
    }
    
    
    // print the navigation link 
    
    echo "<center>" . $first ."&nbsp" . $prev . "&nbspPage $pageNum of&nbsp $maxPage Pages &nbsp" . 
    
    $next ."&nbsp". $last . "</center>"; 
     
    ?>

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    why cant it pass the submitted value? You're retrieving that from $_GET so there is no reason you couldnt have a link that had the search term in as well as the page number

  • #3
    New Coder
    Join Date
    Jun 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for your quick reply, the bit it doesn't work is everything ok when i submit the search, first page display 10 results, but when click "next", nothing but ask me to enter the search term again! why is that?


  •  

    Posting Permissions

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