Click here to Skip to main content
15,397,729 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new to this site and still fairlu new to PHP so not sure if this is were I need to place my question but here goes. I am currently trying to create a filter for my table using PHP.

The filter options are already in my database; e.g I have a species column which currently contains Snake and Lizard. I have managed to get the options to display in a Dropdown list so when selected it correctly filters either Snake or Lizard, they work and filter correctly however the main problem I have is that when I select the option to progress through the filtered pages using the numbered navigation buttons it clears my filter option and shows everything in the database again. Is it possible to make lock the selection until cleared or another item is selected?

i.e. If I select Lizard from the dropdown is it possible to keep this selection until another option is selected.

Just to clarify, I am using PHP.

What I have tried:

<pre lang="PHP"><pre>if (!empty($_POST['dropdown']) && $_POST['dropdown'] == 'snake') {

    
       $sql = "SELECT * FROM animal where species = 'snake' and  user = '$username' LIMIT $start_from,".$limit;

    $all_data=mysqli_query($con,$sql);
    $user_count = mysqli_fetch_row($all_data);   // say total count 9  
    $total_records = $user_count[0];   //9
    $total_pages = ceil($total_records / $limit);    // 9/3=  3
   

}



else if(!empty($_POST['dropdown']) && $_POST['dropdown'] == 'lizard') {

      $sql = "SELECT * FROM animal where species = 'lizard' and  user = '$username' LIMIT $start_from,".$limit;

    $all_data=mysqli_query($con,$sql);     //added 
    $user_count = mysqli_fetch_row($all_data);   // say total count 9  
    $total_records = $user_count[0];   //9
    $total_pages = ceil($total_records / $limit);    // 9/3=  3

}


else { //added 

$sql = "SELECT * FROM animal  where user = '$username' LIMIT $start_from,".$limit;  

    $all_data=mysqli_query($con,"select count(*) from animal where user = '$username'");
    $user_count = mysqli_fetch_row($all_data);   // say total count 9  
    $total_records = $user_count[0];   //9
    $total_pages = ceil($total_records / $limit);    // 9/3=  3

}



$num = 0;


// if($result = mysqli_query($con, $query)){
if($result = mysqli_query($con, $sql)){
    if(mysqli_num_rows($result) > 0){

       echo "<table>";

 
        while($row = mysqli_fetch_array($result)){
            if ($num++ % 4== 0 && $num > 1) echo '</tr><tr>';  
 
                 echo "<td>" . $row['animal'] . "</td>";


         // }
            // echo "</tr>";

        }
         // echo "</table>";
  }

}


    $current_page = isset($_GET['page'])?$_GET['page'] : 1;  

 
    for ($page = $start_page; $page <= $end_page; $page++){
            if ($total_pages > 0) {   

            if ($page == $current_page) {
    
                $active_class = "active";
                   
                 echo"<button class='btn' class='active' a href='room.php?page=".($page)."'>$page</a></button>";
    
    
                 echo "  ";
            } 
            
    
    
            // else {
                // else if ($num == $limit){
                else if ($num == $limit){
    
                echo '<a href="?page='.$page.'" class="btn">'.$page.'</a>';   
                 
                echo "  ";
    
        
    
        }
     
     }
    }
     

       echo "<form id='form_id' method='post' name='myform'>";
        echo "<select name='dropdown'>";
    
        // echo "<option value='All'>All</option>";
    
    if($result = mysqli_query($con, $sql)){
        if(mysqli_num_rows($result) > 0){
            while($row = mysqli_fetch_array($result)){
                  echo "<option name='all'>" . $row['species'] . "</option>";
    
     
        }
         echo "</select>";
         echo "<input id='submit' name='submit' type='submit' value='submit'>";
    
      echo "</form>";
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900