简体   繁体   English

html选择多项选择输入,将它们存储在mysql db中并搜索匹配项

[英]html select multiple choice input, store them in mysql db and search for matches

My question first and then (many...infos): what are my options thinking of usability and speed when i want a user to be able to select mulitple answers from a dropdownbox, save them on their profile, then let other users key in their searchcriteria (can be multiple again) and finally find users by those mutiple searchcriteria? 我的问题首先(很多...信息):当我希望用户能够从下拉框中选择多个答案,将其保存在他们的个人资料中,然后让其他用户输入时,我可以选择考虑可用性和速度他们的searchcriteria(可以再次多次),最后通过那些多个searchcriteria查找用户?

The infos: At the moment i am busy with a part of my website being a matchmaking system, much like datingsites use. 信息:目前我忙于将我的网站的一部分作为配对系统,就像约会网站一样。

(I am using Mysqli, PHP and jQuery for my site) (我在我的网站上使用Mysqli,PHP和jQuery)

I am using a dating site as an example to elaborate on my question: user A fills in his/her profile and enters their preference for a certain car brand: BMW, Volkswagen and Mercedes. 我使用约会网站作为一个例子来详细说明我的问题:用户A填写他/她的个人资料并输入他们对某个汽车品牌的偏好:宝马,大众和梅赛德斯。 User B wants to search the site for members who like BMW and/or Mercedes. 用户B想要在网站上搜索喜欢宝马和/或梅赛德斯的会员。

My original idea was to let user A fill in an input multiple html field and store it as an array (or comma-separated) in one column in my db. 我最初的想法是让用户A填写输入多个html字段并将其作为数组(或以逗号分隔)存储在我的数据库的一列中。 Then figure out how to select-search through that column which has stored arrays in it for user B. 然后找出如何选择 - 搜索通过该列存储数组的用户B。

So basically user A can enter multiple car brands in their profile and user B can give their criteria being also multiple car brands; 因此,基本上用户A可以在他们的个人资料中输入多个汽车品牌,而用户B可以给他们的标准也是多个汽车品牌; the select query should fetch any row for matching criteria. select查询应该获取匹配条件的任何行。

The input-field multiple looks like: 输入字段多个看起来像:

<tr><td>Which car brand(s) do you like?</td><td>
<select multiple name="CarBrandPref[]">
   <option  value="BMW"             > BMW</option>
   <option  value="Ford"            > Ford</option>
   <option  value="Mercedes"        > Mercedes </option>
   <option  value="GMC"             > GMC</option>
   <option  value="Volkswagen"      > Volkswagen </option>
   <option  value="Toyota"          > Toyota </option>
   <option  value="Audi"            > Audi </option>
</select>
</td></tr>

The profile table lay-out would be something like: 配置文件表布局类似于:

 Id     Name            CarbrandPref
--------------------------------------------------------
 1      Mike            bmw,volkswagen,mercedes
 2      Paul            Mercedes, ford, GMC
 3      Axel            GMC, ford, toyota, audi
 4      John            ford, bmw

User B would give searchcriteria 'bmw and mercedes'. 用户B会给searchcriteria'bmw和mercedes'。 The query should then return 3 rows: 然后查询应该返回3行:

Id     Name            CarbrandPref
---------------------------------------------------------
1      Mike            bmw,volkswagen,mercedes
2      Paul            Mercedes, ford, GMC
4      John            ford, bmw

Upon my searches on SO and other sites to get this working i noticed everyone saying that this (storing in arrays in db) was bad for performance etc. etc. Furthermore i can't get the select-search through the arrays in mysql to work. 当我在SO和其他网站上搜索得到这个工作时,我注意到每个人都说这个(存储在db中的数组中)对性能等有害等。此外我无法通过mysql中的数组进行select-search工作。

So now i'm at a loss what to do. 所以现在我不知所措。 I don't want to add a column for each possble carbrand, but what to do else? 我不想为每个可能的carbrand添加一列,但该怎么做? Is there anyone who can help me with this? 有没有人可以帮我这个? Any ideas are much appreciated! 任何想法都非常感谢!

I hope this is clear enough for someone to help me. 我希望这足以让有人帮助我。 If it is not, please tell me and i'll add the requested info. 如果不是,请告诉我,我将添加所需的信息。

I have used these links to be able to store the dropdownbox-populated arrays into the mysql database: 我使用这些链接能够将dropdownbox填充的数组存储到mysql数据库中:

http://www.aleixcortadellas.com/main/2009/03/20/492/ http://www.aleixcortadellas.com/main/2009/03/20/492/

http://toolspot.org/how-to-store-array-mysql.php http://toolspot.org/how-to-store-array-mysql.php

You have three choices. 你有三个选择。

The first is to go down the path you are one and try to compare two comma delimited lists. 首先是沿着你的路径前进并尝试比较两个逗号分隔的列表。 This produces really, really ugly SQL. 这会产生非常非常丑陋的SQL。 It does not allow indexes. 它不允许索引。 The basic idea is something like: 基本想法是这样的:

where find_in_set(substring_index(@list, ',', 1), CarBrands) > 0 or
      find_in_set(substring_index(substring_index(@list, ',', 2), ',', -1), CarBrands) > 0 or
      . . .

The second is to introduce a new table, which has one row for a user and a favorite whatever. 第二个是引入一个新表,它有一行供用户使用,最喜欢的是一行。 Let's make this general, so there is a column for the user, a column for category ("CarBrand") and a name. 让我们做一般,所以有一个用户列,一个类别列(“CarBrand”)和一个名称。 Then the query looks something like: 然后查询看起来像:

from Profile p left outer join
     Favorites f
     on p.userId = f.userId and f.Category = 'CarBrand'
group by p.userId
having sum(f.Name = 'Mercedes') > 0 and
       sum(f.Name = 'BMW') > 0;

The third option is to use full text search and the match function. 第三种选择是使用全文搜索和match功能。 The documentation is here . 文档在这里

My guess is that a full text index is probably the easiest way for you to do what you want. 我的猜测是全文索引可能是你做你想做的最简单的方法。

EDIT: 编辑:

The query about full text index versus option 2. Probably the key question has to do with stop words and short terms. 关于全文索引与选项2的查询。可能关键问题与停用词和短期术语有关。 The full text index, for instance, drops words with less than 4 characters (by default, you can change both of these). 例如,全文索引会删除少于4个字符的单词(默认情况下,您可以更改这两个字符)。 It also has a list of stop words. 它还有一个停用词列表。

My personal inclination would be to use the second option. 我个人倾向于使用第二种选择。 But the reasons may not be good ones. 但原因可能并不好。 It uses standard SQL and I'm comfortable with the data structures and queries. 它使用标准SQL,我对数据结构和查询很满意。 So, it gives me more control over the results. 因此,它让我可以更好地控制结果。

In your case, the full text search is probably easier to implement quickly and would very likely be as fast or faster. 在您的情况下,全文搜索可能更容易快速实现,并且可能会更快或更快。 The full text search also lets you do more complex booleans, such as mixing "AND" and "OR" and doing natural language search to get a relevance value. 全文搜索还允许您执行更复杂的布尔值,例如混合“AND”和“OR”以及进行自然语言搜索以获得相关性值。

You should use a third table to map between users and car marques. 您应该使用第三个表来映射用户和汽车品牌。 The table will have a primary key covering two columns, each of which is a foreign key to one of the other two tables. 该表将有一个主键,覆盖两列,每列都是另外两个表之一的外键。 The presence of a row in this table mapping between "Bastian" and "BMW" means that he has chosen that marque. 此表中存在一行映射“巴斯蒂安”和“宝马”意味着他选择了该品牌。 Absence of such a row means no association exists. 没有这样的行意味着不存在关联。

More specifically, table 1 has users, table 2 has car brands. 更具体地,表1具有用户,表2具有汽车品牌。 each of those have a primary key column with an id #. 每个都有一个id为#的主键列。 The third table links these two. 第三个表链接这两个。 See Many-to-Many Relationships in MySQL and here and here 请参阅MySQL中的多对多关系以及此处此处

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

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