PHP MySQL 搜索表单,具有多个类别的多个复选框

发布于 2024-12-12 04:08:41 字数 602 浏览 0 评论 0原文

首先,这似乎太简单了,不会成为问题——但无论如何。

假设您有一个搜索表单,并且您正在寻找从事某个行业某些分支的公司。该表格将为每个问题提供一组复选框,用户可以选择他们想要的任何金额。


你最喜欢的颜色是什么?

[ ] 红色

[ ] 蓝色

[ ] 黄色

你最喜欢的水果是什么?

[ ] 苹果

[ ] 橙色

[ ] 香蕉


问题编号1

如果用户选中红色和蓝色 - 我们是否从数据库中提取仅与两者匹配的行?或者我们是否包含也只匹配红色或蓝色的行?在我看来,最终用户可能期望看到一种结果或另一种结果,或两者兼而有之!

问题 2

请记住,我必须使用的 CMS 将每个问题的数据存储在数组中。因此,第一个问题的公司 A 可能持有值“红色|蓝色”或“红色”或“红色|蓝色|黄色”等 - 再加上我的表格有 10 个类别,每个类别至少有 5 个复选框 - 我怎样才能将其转换为高效且优化的查询,而不必求助于 if/else 混搭?

非常感谢任何帮助。

This seems too straightforward as first to be a problem - but here is is anyway.

Lets say you have a search form, and you're looking for companies which deal in certain branches of an industry. The form will present a set of set of checkboxes for each question, and the user may choose any amount they want.


What are your favorite colors?

[ ] Red

[ ] Blue

[ ] Yellow

What are your favorite fruits?

[ ] Apple

[ ] Orange

[ ] Banana


Problem No.1

If the user checks red and blue - do we pull from the database rows that match only both? Or do we include rows that also match just red or just blue? In my mind the end-user may expect to see one outcome or the other, or both!

Problem No.2

Baring in mind that the CMS I have to use stores data in arrays for each question. So Company A for the first question may hold values "Red|Blue" or "Red" or "Red|Blue|Yellow" etc - plus the fast that the form I have has 10 categories each with at least 5 checkboxes - how can I translate this into an efficient and optimised query without having to resort to if/else mish mash?

Any help much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℡Ms空城旧梦 2024-12-19 04:08:42

对于 P2,我将创建一个“多对多”表,并使用带有 IN 语句的查询。
对于 P1,类似
例如:

select id,color,count(color) as occurrences from table inner join table_attributes where color in ('red','blue','white') order by occurrences

根据出现次数对结果进行排序的红色、蓝色、白色

For P2, I would make a table "many-to-many", and would use a query with IN statement.
For P1,something like
ex: red,blue,white

select id,color,count(color) as occurrences from table inner join table_attributes where color in ('red','blue','white') order by occurrences

based on the number of occurrences you can sort the results

风铃鹿 2024-12-19 04:08:41
  1. 它很可能是一个连词(红色或蓝色)而不是一个析取(红色和蓝色),但这取决于问题的语义。如果您给出可以同时出现的选项,那么您可能需要析取。

  2. 我看不出比这样更好的方法

    $colours = $POST["颜色[]"];
    $items = array();

    foreach($colours 作为 $colour){

    $items[] = "'$colour' IN 颜色";

    }

    $SQL .= "AND (".implode(' OR ', $items).")";

  1. It's very likely to be a conjunction (red or blue) not a disjunction (red and blue),, but it depends on the semantics of the question. If you give options that can co-occur, then you might need a disjunction.

  2. I can't see a better way than something like

    $colours = $POST["colour[]"];
    $items = array();

    foreach ($colours as $colour) {

    $items[] = "'$colour' IN colours";

    }

    $SQL .= "AND (".implode(' OR ', $items).")";

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文