Home > Software design >  filter table or liste view of data base using php or js
filter table or liste view of data base using php or js

Time:04-22

I have a listeview ( table )that shows data of my database , i want to add filter or search box which helps the user to filter the table ( search his name or his infos make other values invisible ) .. I am a beginner in php and js , could you help me or give any solution i can use it ? thankyou

this is my php code(table):

 <?php



$sql="select * from  association  ORDER BY  id ASC"; 
 $result=mysqli_query($con,$sql); 
 if($result){ 

    $num = 1;


    while($ass = mysqli_fetch_array($result)){
       
      $idAss=$ass['idAss'];
      $nom=$ass['nom'];
      $typeActivite=$ass['typeActivite'];
      $location=$ass['location'];
      $adresse=$ass['adresse'];
      $Admin=$ass['Admin'];
$numTel=$ass['numTel'];
$email=$ass['email'];

 echo ' 

                <tr>
                  <th scope="row">'.$num  .'</th>
        
                  <td>'.$idAss.'</td>
                  <td>'.$nom.'</td>
                  <td>'.$typeActivite.'</td>
                  <td>'.$location.'</td>
                  <td>'.$adresse.'</td>
                  <td>'.$Admin.'</td>
                  <td>'.$numTel.'</td>
                  <td>'.$email.'</td>
                
                
                  <td><button >
                  <i  aria-hidden="true"></i>
                  <a href="update.php?updateid='.$idAss.'" >تعديل</a>
                  </button>
                  </td>
                
                  <td> <button  >
                  <i  aria-hidden="true" red-color></i>
                 
                   <a href="deleteAss.php?deleteid='.$idAss.'" >حذف</a>
                   </button>
                   </td>
                 

                 
                 
                </tr>
        ';
   }     }
?>

and here is my table: click to see the table

CodePudding user response:

This is just an example that will search by a given name, you will need to perfect it and ensure you're safe against SQL Injection.

Create the form in an HTML file, for example form.html:

<html>
<body>

<form action="process_data.php" method="post">
Search by name: <input type="text" name="search_input">
<input type="submit">
</form>

</body>
</html> 

Then in the PHP file you're calling (I called it process_data.php) just add and change these lines at the top:

//THIS IS JUST A SHORT EXAMPLE. IN THE PRODUCTION/FINAL VERSION YOU MUST SANITISE DATA!!

if (isset($_POST['search_input'])) $search = "WHERE nom = '$_POST[search_input]';
$search = mysqli_real_escape_string($con, $search);

$sql="select * from  association $search ORDER BY id ASC"; 
  • Related