我应该如何存储用户定义的选择规则?

发布于 2024-10-18 22:52:09 字数 257 浏览 1 评论 0原文

我需要构建一个应用程序(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 技术交流群。

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

发布评论

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

评论(1

太傻旳人生 2024-10-25 22:52:09

我已经思考这个问题有一段时间了。这就是我的结论:

有一个规则表:

rule_id int,
field1 varchar,
rule1_id int,
operand varchar, --whatever, can be int, if operand is described in other table.
field2 varchar,
rule2_id int

您的示例规则可以这样表达:

rule_id  field1   rule1_id      operand     field2      rule2_id
1       'price'     null            '>'       '100'       null
2       'color'     null            '='       'red'       null
3       null        1               'and'     null        2
4       'price'     null            '>'       '50'        null
5      'color'    null            '='       'green'     null
6       null        4               'and'     null        5
7       null        3               'or'      null        6

然后您将使用 rule_id=7 引用复杂规则。要解决该规则,应递归解决每个其他规则。 field 可以是数据库中的字段,也可以是操作数。然后您就可以根据保存的数据形成 SQL 查询。它也很容易加载并呈现到控件中,因此用户可以使用组合框(HTML

I've been thinking about that for a while. Here's what I came to:

There is a rule table:

rule_id int,
field1 varchar,
rule1_id int,
operand varchar, --whatever, can be int, if operand is described in other table.
field2 varchar,
rule2_id int

Your example rule can be expressed in this way:

rule_id  field1   rule1_id      operand     field2      rule2_id
1       'price'     null            '>'       '100'       null
2       'color'     null            '='       'red'       null
3       null        1               'and'     null        2
4       'price'     null            '>'       '50'        null
5      'color'    null            '='       'green'     null
6       null        4               'and'     null        5
7       null        3               'or'      null        6

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

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