简体   繁体   English

PHP Mysql查询 - 允许用户过滤结果

[英]PHP Mysql query - allow user to filter results

I have a webpage setup with php and mysql. 我有一个php和mysql的网页设置。 I've figured out how to run a query which pulls from mysql and prints the results in a table on the webpage. 我已经想出如何运行从mysql中提取的查询并将结果打印在网页上的表格中。

Now I want to allow the user to filter the results 1 column at a time. 现在我想允许用户一次过滤结果1列。 No typing in needed, a pull down menu which shows options from the database would be fine. 没有需要输入,下拉菜单显示数据库中的选项会很好。 Eg in my code below, they can filter either by name, date, or state. 例如,在我下面的代码中,他们可以按名称,日期或状态进行过滤。 Initially the webpage shows all results. 最初,该网页显示所有结果。

Filtering multiple columns at a time would be nice, but not required. 一次过滤多个列会很好,但不是必需的。 Ideally I envision dropdowns for each column where when the user selects the option, the results update. 理想情况下,我设想每个列的下拉列表,当用户选择该选项时,结果会更新。 I would also prefer to not have to create separate webpages, but if I need to that's fine. 我也不想创建单独的网页,但如果我需要那么好。

My question is: What should I use to accomplish this as simply and as easily as possible? 我的问题是:我应该尽可能简单易用地完成这项工作? I am hoping there is a development tool which makes this an easy task. 我希望有一个开发工具,这使一个简单的任务。 Should I just use HTML get/post forms? 我应该只使用HTML get / post表单吗? Or Ajax? 还是Ajax? Or Javascript? 还是Javascript? RubyonRails? RubyonRails? Or? 要么? Any specific advice on what features within these resources I should use would be helpful also, because I am not familiar with Javascript / Ajax / Ruby, but I am open to diving into it if it has some good built in functionality to do this with. 关于我应该使用这些资源中的哪些功能的任何具体建议也会有所帮助,因为我不熟悉Javascript / Ajax / Ruby,但如果它具有一些良好的内置功能,我愿意深入研究它。

Below is a sample of my code. 下面是我的代码示例。 The query is filtering results because I was testing to make sure I could execute a query with a variable, but initially it should just do a simple select * from table. 查询是过滤结果,因为我正在测试以确保我可以使用变量执行查询,但最初它应该只是从表中执行一个简单的select *。

    <?php 
    print "Filter Results by Name, Date or State";

    $connection = connectdb("mysql");

    $state = "Kentucky";    

    $querytext = sprintf("SELECT * FROM tablename
        WHERE state = '%s'
        ",
        mysql_real_escape_string($state));

    $queryresult = getqueryresult($querytext,$connection);

    $tableid = "mytablestyle";

    printqueryresult($tableid,$queryresult);

    //close connection
    mysql_close($connection);
    ?>

Because you are returning all of the rows and then you want to filter the table you will have to do it with Javascript. 因为您要返回所有行,然后您想要过滤表,您将不得不使用Javascript。 However, if you do not know JavaScript and need a quick solution I would recommend you use JQuery and a plugin-in like this: http://www.picnet.com.au/picnet-table-filter.html 但是,如果您不了解JavaScript并需要快速解决方案,我建议您使用JQuery和插件,如下所示: http//www.picnet.com.au/picnet-table-filter.html

If you look at the demo there is a list drop down like the one you described. 如果您查看演示,则会有一个列表下拉,就像您描述的那样。 Good luck! 祝好运!

If I understand correctly, you would like that when the value of the dropdown list changes then it would update filtered data according to the selected value? 如果我理解正确,您希望当下拉列表的值发生变化时,它会根据所选值更新过滤后的数据吗? For this you would want to use javascript, onchange event. 为此你想要使用javascript,onchange事件。 You can use vanilla javascript, of course, but I (and probably any other developer) prefer jQuery . 当然,您可以使用vanilla javascript,但我(可能还有其他开发人员)更喜欢使用jQuery

I've put up a little example at jsFiddle : 我在jsFiddle上写了一个小例子:

$(function(){
    $("#my-select").change(function(){
        //$("form").submit();
        alert("submitting the form now!");
    });
});

<form>
    <select name="something" id="my-select">
            <option value="one">One</option>
            <option value="two">Two</option>
    </select>
</form>

You have numerous options, each with their own advantages and disadvantages. 您有多种选择,每种选择都有各自的优缺点。

1 Would be regular html forms with get/post. 1将是带有get / post的常规html表单。 This would be the simplest to set up if you have no js knowledge, but would require a page refresh each change 如果您没有js知识,这将是最简单的设置,但每次更改都需要页面刷新

2 Would be to use ajax to do the same without page reload. 2将使用ajax在没有页面重新加载的情况下执行相同的操作。 your php would be pretty much identical to solution one. 你的PHP几乎与解决方案一致。

3 Would be to load all the data (select *) into a js variable on page load, and sort it as needed with js. 3将在页面加载时将所有数据(select *)加载到js变量中,并根据需要使用js对其进行排序。 This would mean you only make one database call, but would be unsuitable if a lot of data is returned 这意味着您只进行一次数据库调用,但如果返回大量数据则不适合

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在MySQL联接查询的结果上过滤v-for - Filter v-for on results of mysql join query Firebase Firestore 仅允许用户在有过滤器的情况下查询集合 - Firebase Firestore only allow user to query a collection if there is a filter 处理从PHP页面返回的MYSQL查询结果 - Handling MYSQL Query Results returned from PHP page PHP / MYSQL查询HTML表单字段中的打印结果 - PHP/MYSQL Query Print Results in HTML Form Field PHP / MySQL搜索结果-在类型&gt; 1时过滤结果,而在其他类型不&gt; 1的地方保持结果-Codeigniter - php/mysql search results - filter result when type >1 while keeping results where other types not >1 - codeigniter 使用同一页面上的复选框或单选按钮的 AJAX 过滤 php MySQL 结果 - AJAX filter php MySQL results using checkboxes or radio button on same page 将 PHP/MySql 结果放入 Javascript - Putting PHP/MySql Results into Javascript mongodb查询:使用字段上的过滤器获取N个结果 - mongodb query : getting N results with a filter on a field 如何过滤流星数据库查询结果? - How to filter Meteor database query results? 如何从PHP中的mysql数据库动态显示/生成查询结果表 - How to dynamically display/generate table for query results from a mysql database in PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM