简体   繁体   English

PHP和MySQL用户过滤

[英]PHP and MySQL user filtering

I have a web application that shows a list of events taken from a mysql db and presented to a user. 我有一个Web应用程序,它显示从mysql数据库获取并呈现给用户的事件列表。

The user can then filter the results according to several (8-10) 'types' 然后,用户可以根据几种(8-10)'类型'过滤结果

My question is, how should I approach this? 我的问题是,我应该如何处理? Basically what I need to do is take the user input for the filter (in $_GET ) and then find out which types he is looking to filter and according to that, change my sql query. 基本上,我需要做的是获取用户输入的过滤器(在$_GET ),然后找出他要过滤的类型,并据此更改我的sql查询。

How do I do it in a way that is not very complex? 如何以一种不太复杂的方式来做呢? I thought of using many 'if' or 'case' statements, but i'm not sure if that's the best solution as its a lot of coding for a very simple thing.. 我曾考虑使用许多'if'或'case'语句,但是我不确定这是否是最好的解决方案,因为它为非常简单的事情进行了大量编码。

Can anyone give me some advice? 谁能给我一些建议? Thanks, 谢谢,

Example DB: 示例数据库:

EVENT_ID    EVENT_NAME     EVENT_TYPE
1           test           regular
2           test2          vip
3           test4          testtype

... ...

Example $_GET: 示例$ _GET:

$_GET['regular'] = 'on'

... ...

Is it supposed to allow multiply selections or only 1 filter selection. 它应该允许乘法选择还是仅允许1个过滤器选择。 If 1 you could use the case function easy or you could use the onchange function of javascript. 如果为1,则可以使用case函数easy或使用javascript的onchange函数。

you should probably create an array of types using checkboxes 您可能应该使用复选框创建类型数组

<input type="checkbox" name="types[]" value="regular" /> Regular

<input type="checkbox" name="types[]" value="vip" /> VIP

then you would have an array of types with $_REQUEST['types']... 那么您将拥有带有$ _REQUEST ['types']的类型数组...

echo implode(",",$_REQUEST['types]); echo implode(“,”,$ _ REQUEST ['types]); // for example // 例如

Well it depends if you actually store event_types as string (you shouldnt). 好吧,这取决于您是否实际将event_types存储为字符串(不应该)。

If you change them to int you could simply do 如果将它们更改为int,则只需

$query = "select * from events";

if(isset($_REQUEST['types'])){
    $query .= "and event_types in (".implode(",",$_REQUEST['types']).")";
}

If you keep them as string in you db you could do the same thing but you'll have to had '' around the values... 如果您在数据库中将它们保留为字符串,则可以执行相同的操作,但是必须在值周围添加''。

of course you'll have to secure this 当然,您必须确保这一点

Takes GET from a number of checkboxes and creates SQL query. 从多个复选框中获取GET并创建SQL查询。 You need to change column names and the $getOptions array. 您需要更改列名和$ getOptions数组。 The key of a $getOptions element is the name of the type. $ getOptions元素的键是类型的名称。

$getOptions = array(
    'regular'    => $_GET['regular'],
    'vip'        => $_GET['vip'],
    'testtype'   => $_GET['testtype']
);

$sql = "SELECT * FROM events";

$sqlOptions = array();

foreach($getOptions as $key => $value)
{
    if($value == 'on')
    {
        $sqlOptions[] = $key;
    }
}

if(!empty($sqlOptions))
{
    $sql .= " WHERE EVENT_TYPE IN ('" . implode("','", $sqlOptions) . "')";
}

// Execute $sql

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM