SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Search result pagination return all result when next is clicked

    Hi all,
    I'am trying to build a basic search but am getting a small code issue here, i want to paginate my searched result set and this is what i have 1) an input field named "input", a and within a form of post method 2) when user submits the form it calls search.php with the follow code bellow.
    The problem

    the pagination returns a properly paginated result set but when i move to next page all records in the search table are returned including those that do not meet the criteria in the where clause.

    Here is the code:
    Thanks guys
    PHP Code:
    <?php
    $input 
    $_POST['input'];
    $categories $_POST['category'];
    $state $_POST['state'];

    $targetpage "search.php";     
    $limit 3;

    //This query checks for data    
    $qq " SELECT * FROM classified where confirm='0' ";
    $qq $db->prepare($qq);
    $qq->execute();

    $total_pages =$qq->rowCount();

    $stages 3;
    $page = ($_GET['page']);
    if(
    $page){
    $start = ($page 1) * $limit
    }else{
    $start 0
    }

    $rows $qq->fetchAll();
    if (
    $rows 0){
    $q " SELECT * FROM classified where confirm='0' ";     
    if(!empty( 
    $input)) {
    $q .= "AND title LIKE '%".$input."%' ";
    }
    if (!empty(
    $_POST['search_category']) )
    {
    $q .= "AND id_cat = ".$categories." ";
    }

    if (!empty(
    $_POST['state']) )
    {
    $q .= "AND id_state = ".$state." ";
    }
    $q .= "ORDER BY date DESC LIMIT $start$limit ";
    }
    $q $db->prepare($q);
    $q->execute();
    //// Just for testing purposes to see what's coming out
    print_r($q);

    /*** echo number of columns ***/
    $resultt $q->fetchAll();
    // Initial page num setup
    if ($page == 0){$page 1;}
    $prev $page 1;  
    $next $page 1;  

    $lastpage ceil($total_pages/$limit);      
    $LastPagem1 $lastpage 1;                    


    $paginate '';
    if(
    $lastpage 1)
    {   
        
    $paginate .= "<div class='paginate'>";
        
    // Previous
        
    if ($page 1){
            
    $paginate.= "<a href='$targetpage?page=$prev'>Previous</a>";
        }else{
            
    $paginate.= "<span class='disabled'>Previous</span>";   }

            
    // Pages    
    if ($lastpage + ($stages 2))  // Not enough pages to breaking it up
        
    {   
            for (
    $counter 1$counter <= $lastpage$counter++)
            {
    if (
    $counter == $page){
    $paginate.= "<span  class='current'>$counter</span>";
    }else{
    $paginate.= "<a    

        ref='
    $targetpage?page=$counter'>$counter</a>";}                 
            }
        }
        elseif(
    $lastpage + ($stages 2))   // Enough pages to hide a few?
        
    {
            
    // Beginning only hide later pages
            
    if($page + ($stages 2))       
            {
    for (
    $counter 1$counter + ($stages 2); $counter++)
                {
    if (
    $counter == $page){
    $paginate.= "<span class='current'>$counter</span>";
    }else{
    $paginate.= "<a   

       href='
    $targetpage?page=$counter'>$counter</a>";}                 
                }
                
    $paginate.= "...";
                
    $paginate.= "<a 

       href='
    $targetpage?page=$LastPagem1'>$LastPagem1</a>";
                
    $paginate.= "<a 

       href='
    $targetpage?page=$lastpage'>$lastpage</a>";        
            }
            
    // Middle hide some front and some back
            
    elseif($lastpage - ($stages 2) > $page && $page > ($stages 2))
            {
    $paginate.= "<a href='$targetpage?page=1'>1</a>";
    $paginate.= "<a href='$targetpage?page=2'>2</a>";
    $paginate.= "...";
    for (
    $counter $page $stages$counter <= $page $stages$counter++)
                {
    if (
    $counter == $page){
    $paginate.= "<span class='current'>$counter</span>";
        }else{
    $paginate.= "<a 

        href='
    $targetpage?page=$counter'>$counter</a>";
        }                   
    }
    $paginate.= "...";
    $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
    $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
            }
    // End only hide early pages
    else
    {
    $paginate.= "<a href='$targetpage?page=1'>1</a>";
    $paginate.= "<a href='$targetpage?page=2'>2</a>";
    $paginate.= "...";
    for (
    $counter $lastpage - (+ ($stages 2)); $counter <= $lastpage$counter++)
            {
    if (
    $counter == $page){
    $paginate.= "<span class='current'>$counter</span>";
    }else{
        
    $paginate.= "<a 
        href='
    $targetpage?page=$counter'>$counter</a>";
        }                   
    }
    }
    }
    // Next
    if ($page $counter 1){ 
    $paginate.= "<a href='$targetpage?page=$next'>Next</a>";
    }else{
    $paginate.= "<span class='disabled'>Next</span>";
    }
    $paginate.= "</div>";       

    }
        
    // pagination
        
    echo $total_pages.' Results';
        echo 
    $paginate;
        
    ////////
       
    if (count($resultt) !== 0) {
       foreach(
    $resultt as $row) {

       echo  
    $row['title'];
       echo  
    $row['categories'];
       echo  
    $row['state'];

    }
    }else{
    echo 
    "No data available";
    }   
        
    ?>

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    As you are seemingly building an SQL query string partly dependent upon values in a GET string request your first port of call should be to carefully review the output of :

    PHP Code:
    var_dump($_GET)? 
    after pressing your `next` link ... and then follow those variables through your script till you find the culprit.

    You need to learn how to do this kind of debugging yourself before posting reams of code, it will be faster in the long run.

  3. #3
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, but I'm really lost here

  4. #4
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone??!!

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Just briefly glancing at the code, my first response is "You're building your WHERE clause based on POST results - but when a user clicks on a link, there are no POST results anymore."
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Tags for this Thread

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
  •