为mysql,php表创建过滤器

I am trying to create a filter tab for my table that will have a few different options when clicked filter the data from the table and display a new set of results, a single selection from each drop down will then filter table results. Have tried a few tutorials to no avail(I am a beginner with PHP, js)

Here is my code;

Display Table on page;

<?php 
$con=mysqli_connect("localhost","root", "", "database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM table GROUP BY id DESC LIMIT 25");

if (!$result) {
    printf("Error: %s
", mysqli_error($con));
    exit();
}

echo "<table border='5' class='table'>
<tr>
<th>Game ID</th>
<th>Game Name</th>
<th>Category</th>
<th>Suggested LVL</th>
<th>Insert Date</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['gamename'] . "</td>";
echo "<td>" . $row['gamecategory'] . "</td>";
echo "<td>" . $row['gamelevel'] . "</td>";
echo "<td>" . $row['listdate'] . "</td>";
echo "</tr>";
}
echo "</table>";
    ?>

What I would like the filter to be like; It is just an example, and the cars have nothing to do with my game database.

    <div class="input-group">
<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
    </div>

Im just kind of stumped on where to go from here, is there any handy free jquery plugins, AJAX, or any other options?

Thanks for the help

Just coming back to say that this is possible by re-querying the data from the database on change of the select option.

After working with several different frameworks I have found that by doing this in a javascript framework becomes VERY simple and easy to understand(a good option is angularjs). However by wrapping the original select list

   <div class="input-group">
<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
    </div>

in a form tag like so and adding the name attr to the select tag:

<form action="urltoscript" method="POST">
   <div class="input-group">
<select name="changingValues">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
    </div>
    <input type="submit" name="buttonSubmit" />
</form>

would allow you to retrieve the selected value of each item on the backend using PHP taking advantage of isset isset($_POST["changingValues"]) and then requerying the data as the $result. You will be refreshing the page after each change and must click the submit button unless you incorporated some AJAX calls using javascript.

Again I recommend using a JS or PHP framework to accomplish filters and such, but this way should work and I wish I knew this back when the question was written.

You need a more flexible way of constructing your query. A framework would make this easy. This example is using Laravel.

$q = DB::table('table')->groupBy('id');

if ( isset($_POST['color']) ) {
    $q = $q->where('some-column', $_POST['color']);
}

if ( isset($_POST['car']) ) {
    $q = $q->where('some-other-column', $_POST['car']);
}

$results = $q->get();

Note that Laravel's DB class will automatically filter the variables that get passed to it.

You can do the same thing using straight PHP but it is going to require that you write a lot of boilterplate.

// Store user submitted filters in an array
$where = array();

if ( isset($_POST['color']) ) {
    $where[] = "some-column = '".validate_your_user_inputs($_POST['color'])."'";
}

if ( isset($_POST['car']) ) {
    $where[] = "some-other-column = '".validate_your_user_inputs($_POST['car'])."'";
}

// Create WHERE clause or empty string that gets put into the SQL
if ( sizeof($where) > 0 ) {
    $where = ' WHERE '.implode(' AND ', $where);
} else {
    $where = '';
}

$q = sprintf('SELECT * FROM table %s GROUP BY id LIMIT 25', $where);

That's obviously a very simple example and it ignores the validation that needs to occur before you pass variables directly to MySQL, but it should get you started.