douxie0824 2014-12-15 03:48
浏览 44
已采纳

为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

  • 写回答

2条回答 默认 最新

  • douyi9597 2016-01-04 21:21
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分