简体   繁体   English

带运算符的PHP查询/比较方法

[英]PHP Query/Comparison Method with Operators

My PHP page has a range of variables on each load; 我的PHP页面在每次加载时都有一系列变量; their values determine what widgets are displayed. 它们的值确定要显示的窗口小部件。 Each widget has set criteria for when it should display. 每个窗口小部件都为何时显示设置了标准。

Example. 例。 Display widget 'A' where: 显示小部件“ A”,其中:

"page==1, page!=3, user>9, rank!=blue" “ page == 1,page!= 3,用户> 9,等级!= blue”

$myVars = array();
$myVars['page'] = 1;
$myVars['user'] = 99;
$myVars['rank'] = 'red';

In this scenario I've written a simple method - it delimits the query and does a comparison on each criteria and stops as soon as a FALSE is encountered. 在这种情况下,我编写了一个简单的方法-它对查询定界,并对每个条件进行比较,并在遇到FALSE时立即停止。 The problem is there are no "OR" operators supported by my engine. 问题是我的引擎不支持“或”运算符。 Every criteria must be met. 必须满足所有标准。 And I cannot get my head around how the operators could be added to it. 而且我无法理解如何添加运算符。

"page==1 || page==2 && page!=3 && user>9 || user==3 && rank !=blue" “ page == 1 || page == 2 && page!= 3 &&用户> 9 || user == 3 &&等级!=蓝色”

This would give me much more flexibility on when a widget would display, without making lots of duplicate widgets for different scenarios. 这将使我在显示小部件的时间时更具灵活性,而无需为不同的场景制作大量重复的小部件。 I see a parallel between how SQL must run queries and how I want to query my variables - is there a prewritten engine I can use or is my fate set in to writing a complete engine to work out if a widget has met criteria? 我看到SQL必须如何运行查询与我想如何查询变量之间存在相似之处-是否可以使用预写引擎,或者如果小部件符合条件,我的命运是否可以编写一个完整的引擎来解决?

Thanks in advance, Craig. 预先感谢,克雷格。

Extend your language to support Conjunctive Normal Form or Disjunctive Normal Form 扩展语言以支持合取范式析取范式

page==1 || page==2 && page!=3 && user>9 || user==3 && rank !=blue

Will look like (CNF): 看起来像(CNF):

(page==1 || page==2) && page!=3 && (user>9 || user==3) && rank !=blue

If you want to make parsing simpler you can do: 如果要简化解析,可以执行以下操作:

(page==1 || page==2) && (page!=3) && (user>9 || user==3) && (rank !=blue)

DNF would look like: DNF看起来像:

(page==1 && page!=3 && user>9 && rank!=blue)
||
(page==2 && page!=3 && user>9 && rank!=blue)
||
(page==1 && page!=3 && user==3 && rank!=blue)
||
(page==2 && page!=3 && user==3 && rank!=blue)

CNF seems more natural here. CNF在这里似乎更自然。

另一个可行的解决方案(比eval更安全)可能是使用parse_str的某种组合将动态变量转换为可比较的变量-但是我不确定如何在没有eval的情况下仍然可以生成动态if语句。

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

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