#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Error in pagination with filters


    Hello, I have a pagination with filters and it works more or less well. The problem is that for example if I select one option in one of the filters it works for the first page but if I click in the second page It brings me the second page of the default results no the good second page with the filter option selected.
    I tried several things to repair it, but I cannot , I put my code to show you better,

    I have these two foreach :

    PHP Code:
    $numGet = array("page","pagenum");

    foreach(
    $_GET as $k=>$v) {
        if (
    in_array($k$numGet) ) {
            
    $_GET[$k] = cleanInput($v3"numeric");
        } else {
            
    $_GET[$k] = cleanInput($v3);
        }
    }

    foreach(
    $_POST as $k=>$v) {
        
    $v substr($v050);
        
    $_POST[$k] = cleanHex($v);

    After I have my filters(11), I am only going to put someones to do clearer the post:
    PHP Code:
    //Brand
    if (isset ($_POST['BrandInput'])) {
        
    $BrandInput $_POST['BrandInput'];
        
        if (
    $BrandInput !=''){
            if(
    $DataFilter!="") {
                
    $DataFilter .= " AND ";
            }
            
    $DataFilter .= "brand_id = '" $BrandInput ."' ";
        }
    } else {
        
    $BrandInput ''

    This last filter is without $_GET, but after I found out that to the second page is necessay to use $_GET inspite of $_POST and I tried to put this in other filter like this:

    PHP Code:
    //Model
    if (isset ($_POST['ModelInput'])) {
        
        if(
    $beginning 0){
            
    $ModelInput $_POST['ModelInput'];
        }else{
            
    $beginning = (($getPage 1) *$SetNumberOnPage);
            
    $ModelInput $_GET['ModelInput'];
        }
        if (
    $ModelInput !=''){
            if(
    $DataFilter!="") {
                
    $DataFilter .= " AND ";
            }
            
    $DataFilter .= "Body_id = '" $ModelInput ."' ";
        }
    } else {
        
    $ModelInput ''

    But it doesn't work neither

    This is a part of the pagination:

    PHP Code:
    $qryCarNumber str_replace(".Filter."$DataFilter$qryCarNumber);

    $rcsCarNumber $cardata->query($qryCarNumber);
    $row $rcsCarNumber->fetch_array(MYSQLI_BOTH);

    $total_pages floor($row["VehicleCount"]/$SetNumberOnPage)+1;

    if(
    is_numeric($getPage)){
        if(
    $getPage $total_pages){
            
    $getPage $total_pages;
        }
        
    $beginning = (($getPage 1) *$SetNumberOnPage);
    } else {
        
    $beginning 0;
        
    $getPage 1;                                             

    That at the beginning I set this part before the filters but I realised that the VehicleCount was always the same.

    This is how I show the select:

    PHP Code:
            <select name="BrandInput">
                                           <option></option>
                                           <?php 
                                           $rcsBrand 
    $cardata->query($qryBrand);
        
                                           while (
    $row $rcsBrand->fetch_array(MYSQLI_BOTH)) {
                                                  
                                               if (
    $row["BrandNumberPK"] == $BrandInput) {
                                                
    $BrandSelected "selected";
                                            } else {
                                                
    $BrandSelected "";
                                            }
                                                  
                                            
    ?><Option <?php echo $BrandSelected?> value="<?php echo $row["BrandNumberPK"]; ?>"><?php echo $row["BrandDescription"]; ?></option>
                                           <?php
                                        
    }    
                                        echo 
    "</select>";
                                        
    $rcsBrand -> free_result();      
                                        
    ?>                                      
                                       </select>
    I don't know very well if I have to set free_result() or not, in others filters I didn't set and it doesn't work neither.

    And to show the numbers of the pages I built this code:

    PHP Code:
    //Print previous HR 
                                    
    if (($total_pages 1) AND ($getPage )) {
                                        echo 
    "<a href ='{$_SERVER['PHP_SELF']}?page=".($getPage-1)."'>" .$arrMainPage[$MainPagePrevious]['TextHeader']. "</a> "
                                    }    
                                    
                                    
    //If current page is < 5 the list start in 1 if not, in current page - 5 HR
                                    
    if ($getPage 5) {
                                        
    $StartList 1;
                                    } else {
                                        
    $StartList $getPage 5;
                                    }

                                    
    // If current page is very near of total_pages, StartList = $total_pages - 10; HR
                                    
    if ((($getPage 5) > $total_pages) AND ($total_pages >1)) {     //ADDED-> AND ($total_pages >1)
                                        
    $StartList $total_pages 10;
                                    }
                                    
                                    
    //New if HR
                                    //if (($getPage == $total_pages) AND ($total_pages<2)){
                                    
    if (($total_pages 0) OR ($getPage 0)){                         ////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE///
                                        
    echo "1";                                                    ////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE///
                                    
    }else{                                                            ////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE///
                                    //This for was out of the new if HR
                                        
    for($cont $StartList$cont <= $StartList 10$cont++){ 
                                            if (
    $cont == $getPage) {
                                                   echo 
    "<a class='bold' href ='{$_SERVER['PHP_SELF']}?page=".$cont."'>$cont</a> ";
                                            } else {
                                                if(
    $total_pages 1){                 ////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE///
                                                       
    echo "<a href ='{$_SERVER['PHP_SELF']}?page=".$cont."'>$cont</a> ";
                                                }    
                                            } 
                                           }
                                    }    

                                    
    //Next button
                                       
    if(($getPage $total_pages) AND ($total_pages )){
                                           echo 
    "<a href ='{$_SERVER['PHP_SELF']}?page=".($getPage+1)."'>" .$arrMainPage[$MainPageNext]['TextHeader']. "</a> ";
                                       } 
    I have also this script for the pagination:

    Code:
    <script>
    	function Number_Cars_Page() {
    			    self.location = window.location.pathname +'?page=1&pagenum='+$("#CarsPerPage").val()
    			};
    </script>
    So, to summarize, I cannot show well the second page when I try to put a option of one of the filters.

    Anybody know where is my mistake?

    Sorry for my english and sorry for my not good level of php, I am trying to learn by myself, I read a lot about it but sometimes I look for a lot for one solution and not find anything, sometimes I am really blocked.

    Thank you very much.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    Your filters use GET, which means the filters are expected in the URL. However, when you print your pager, you do this:
    PHP Code:
                                                       echo "<a href ='{$_SERVER['PHP_SELF']}?page=".$cont."'>$cont</a> "
    The filters aren't included in the URL! You must include the filters in the URL of the page numbers if you want them to be carried over to the next page
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Advancing


    Originally Posted by ManiacDan
    Your filters use GET, which means the filters are expected in the URL. However, when you print your pager, you do this:
    PHP Code:
                                                       echo "<a href ='{$_SERVER['PHP_SELF']}?page=".$cont."'>$cont</a> "
    The filters aren't included in the URL! You must include the filters in the URL of the page numbers if you want them to be carried over to the next page
    Thank you very very much for your answer ManiacDan.

    I tried to do what you told me but I don't know very well if is correct the code, probably not because it doesn't work :P, I did it in this way, and I do a lot of different ways:

    PHP Code:
    //if (($getPage == $total_pages) AND ($total_pages<2)){
                                    
    if (($total_pages 0) OR ($getPage 0)){                         ////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE///
                                        
    echo "1";                                                    ////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE///
                                    
    }else{                                                            ////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE///
                                    //This for was out of the new if HR
                                        
    for($cont $StartList$cont <= $StartList 10$cont++){ 
                                            if (
    $cont == $getPage) {
                                                   echo 
    "<a class='bold' href ='{$_SERVER['PHP_SELF']}?page=".$cont."&DataFilter=".$ModelInput."'>$cont</a>";
                                                
    //echo "<a class='bold' href ='Screen2List2.php?page=".$cont."&ModelInput=".$ModelInput."'>$cont</a>";
                                                //echo "<a class='bold' href ='{$_SERVER['PHP_SELF']}?page=".$cont."&ModelInput=".$ModelInput."'>$cont</a>"; //with this ModelInput is 7 in the second page when I choose one option, so in this way I get to transefer something
                                                //echo "<a class='bold' href ='{$_SERVER['PHP_SELF']}?page=".$cont."&DataFilter=".""."'>$cont</a>";
                                                //echo "<a class='bold' href ='Screen2List2.php?page=".$cont."&DataFilter=".""."'>$cont</a>";
                                            
    } else {
                                                if(
    $total_pages 1){                 ////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE/////NEW LINE///
                                                       
    echo "<a href ='{$_SERVER['PHP_SELF']}?page=".$cont."&DataFilter=".$ModelInput."'>$cont</a>";
                                                    
    //echo "<a href ='Screen2List2.php?page=".$cont."&ModelInput=".$ModelInput."'>$cont</a>";
                                                    //echo "<a href ='{$_SERVER['PHP_SELF']}?page=".$cont."&ModelInput=".$ModelInput."'>$cont</a>"; //with this ModelInput is 7 in the second page when I choose one option, so in this way I get to transefer something
                                                    //echo "<a href ='Screen2List2.php?page=".$cont."&DataFilter=".""."'>$cont</a>";
                                                    //echo "<a href ='Screen2List2.php?page=".$cont."&DataFilter=".$DataFilter."'>$cont</a>";
                                                
    }    
                                            } 
                                           }
                                    } 
    But it doesn't work, what am I doing wrong?
    I don't know very well if I have to use Screen2List2.php or {$_SERVER['PHP_SELF']} or is the same? In spite of this I tried with both like you can see and it doesn't work neither.

    What I was changing also is my filter ModelInput the part of the if with the GET like I put in the other post above, so have I put the part of the GET in all filters or is well like they are?

    In somes of the stuff that I tried you can se my explication, and it works a little because above in the explorer get a value for ModelInput with a number but for some reason the elements showed are the same that the second page of the default values:

    localhost/Screen2List2.php?page=2&DataFilter=13

    Thank you very much again
  6. #4
  7. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    If the user is able to change the filters as they go then what you'll end up wanting to do is have a hidden field in the form denoting the page, then use javascript to update that and submit the form when the user clicks on a new page number.

    The form should also be method="GET".

    The downside to Dan's approach is that you'd be defining the filters in the link when the page loads and they'd remain the same until clicked, so even if the user changed a filter setting, the link wouldn't include that change when clicked.
    LinkedIn: Dave Mittner
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by dmittner
    If the user is able to change the filters as they go then what you'll end up wanting to do is have a hidden field in the form denoting the page, then use javascript to update that and submit the form when the user clicks on a new page number.

    The form should also be method="GET".

    The downside to Dan's approach is that you'd be defining the filters in the link when the page loads and they'd remain the same until clicked, so even if the user changed a filter setting, the link wouldn't include that change when clicked.
    Thank you for your answer again dmittner, I finally resolved it using the comand $_SESSION, I put my solution for everybody, I changed all my filters including the comand $_SESSION, this one of them:

    PHP Code:
    //Category
    if (isset ($_POST['CategoryInput'])) {
        
    $CategoryInput $_POST['CategoryInput'];
    } elseif (isset( 
    $_SESSION['filter']['CategoryInput'] )) {
        
    $CategoryInput $_SESSION['filter']['CategoryInput'];
    } else {
    //Nothing
    }

    if (isset(
    $CategoryInput)) {

        if (
    $CategoryInput !=''){
            
    $_SESSION['filter']['CategoryInput'] = $CategoryInput;                                                    
            if(
    $DataFilter!="") {
                
    $DataFilter .= " AND ";
            }
            
    $DataFilter .= "category_id = '" $CategoryInput "' ";
        }     
    }else {
        
    $CategoryInput '';

    And the line of the code of my form, I changed the part of the action in this way:

    Code:
    <form method="POST" action="<?=$_SERVER['REQUEST_URI']?>">
    Maybe is useful for someboy also.

    Thank you very much for the answers
  10. #6
  11. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by Improving2013
    Thank you for your answer again dmittner, I finally resolved it using the comand $_SESSION, I put my solution for everybody, I changed all my filters including the comand $_SESSION, this one of them:
    That'll get the job done, just keep in mind that it prohibits being able to share a URL with others specifically to your results, since the URL you have doesn't include all the factors.
    LinkedIn: Dave Mittner

IMN logo majestic logo threadwatch logo seochat tools logo