dongyu1125 2010-12-01 09:49
浏览 47
已采纳

PHP和MySQL用户过滤

I have a web application that shows a list of events taken from a mysql db and presented to a user.

The user can then filter the results according to several (8-10) 'types'

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.

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..

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['regular'] = 'on'

...

  • 写回答

3条回答 默认 最新

  • dongtui4038 2010-12-01 10:06
    关注

    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']...

    echo implode(",",$_REQUEST['types]); // for example

    Well it depends if you actually store event_types as string (you shouldnt).

    If you change them to int you could simply do

    $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

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

报告相同问题?

悬赏问题

  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在