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.

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 "&nbsp;&nbsp;";
            } 



            // else {
                // else if ($num == $limit){
                else if ($num == $limit){

                echo '<a href="?page='.$page.'" class="btn">'.$page.'</a>';   

                echo "&nbsp;&nbsp;";



        }

     }
    }


       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>";

Recommended Answers

All 4 Replies

So sorry for taking so long to see this topic and respond to it.

I would switch $_POST['dropdown'] to $_REQUEST['dropdown'] everywhere in your code, which means that the dropdown option is not going to be passed in by a form POST submission, but alternatively just by a query string.

Then change the form on line 101 to read echo "<form id='form_id' method='get' name='myform'>";

Then, on lines 77 and 89 of your code, instead of just passing in the page number, also pass in the dropdown choice, so as you navigate code, the dropdown choice gets sent along with the page number, as so:

echo '<a href="?dropdown=' . $_REQUEST['dropdown'] . '&amp;page='.$page.'" class="btn">'.$page.'</a>';   

Good luck, and sorry it took me a few days to get back to you here! I'm usually much quicker with responding to PHP questions, but I didn't get much sleep over the past week and wasn't at my desk all that much.

hello good post )

thank you for the help unfortunately I lost the majority of my coding due to a hardware issue on my laptop. I was just wondering how to make my echo options selectable so when the name is selected it only shows the name?

?php

$s = "SELECT name from users";
$result = $con->query($s);

        echo "<form id='id' method='get' name='myform'>";

        echo "<select name='fetch' class='filter'>";


        // echo "<option value='All'>All</option>";

    if($result = mysqli_query($con, $s)){
        if(mysqli_num_rows($result) > 0){

            while($row = mysqli_fetch_array($result)){
                  echo "<option>" . $row['name'] . "</option>";


        }

        echo "</select>";

        echo "</form>";

      }
}

$sql .= "ORDER BY rating DESC, player _ number ASC";
for more reverent coding check this link

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.