我应该如何存储用户定义的选择规则?
我需要构建一个应用程序(PHP/MySQL,但我认为这并不重要),让用户输入一组与特定产品匹配的规则。这应该看起来像一个表单,允许选择一个键并输入其值,但支持使用 OR 和 AND 语句进行分组。例如,用户应该能够输入以下产品规则:(价格超过 100 且红色)或(价格超过 50 且绿色)
、重量超过 2
以及很快。问题是,我不确定应该如何将其存储在数据库中,也不知道如何为此编写 UI 代码。有没有通用的方法来解决这样的问题?
谢谢
I need to build an app (PHP/MySQL but I don't think it matters), letting a user to input a set of rules matching particular products. That should look like a form, allowing to select a key and to input its value, but supporting grouping with OR and AND statements. For example the user should be able to input the following product rules: (price over 100 and red color) or (price over 50 and green color)
, weight over 2
and so on. The problem is, I'm not sure how should I store that in the database and not sure how to code UI for that. Is there any common approach to solving such a problem?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经思考这个问题有一段时间了。这就是我的结论:
有一个规则表:
您的示例规则可以这样表达:
然后您将使用
rule_id
=7 引用复杂规则。要解决该规则,应递归解决每个其他规则。field
可以是数据库中的字段,也可以是操作数
。然后您就可以根据保存的数据形成 SQL 查询。它也很容易加载并呈现到控件中,因此用户可以使用组合框(HTMLI've been thinking about that for a while. Here's what I came to:
There is a rule table:
Your example rule can be expressed in this way:
You will then be referencing the complex rule with
rule_id
=7. To resolve that rule, each other rule should be resolved recursively.field
can be a field in a database, as well as theoperand
. You would then be able to form SQL queries from this saved data. It's also easy to load and present into controls, so the user can edit this rule using combo boxes (HTML<select>
) and stuff.