我很高兴只是在我网站的页面上到处添加一个 if 语句来生成一个数据列表,但现在它有 000 行代码而且很乱。
我确信我正在做的事情有更好的逻辑,所以任何帮助都会很好。
这是我的逻辑(或者应该如何运作)
check access lvl and add to sql - `accesslvl` = '1'
Check if get_status is set - if set add to sql
check if get_product is set - if set add to sql
check if get_compnay is set - if set add to sql
check if get_datefrom is set - if set add to sql
check if get_dateto is set - if set add to sql
运行查询
现在我已经使用了很多嵌套的 div 来计算是否设置了 gets,如果没有将它们传递给 sql,它还必须计算出是否需要 WHERE 或 AND。
我找到了这篇文章 creating a mysql search string dynamically?
我认为这可能会有帮助,但不确定。 我的代码示例。
if (isset($_GET['product'])&& $_GET['product'] >0){
$product = $_GET['product'];
if($a == 1){
$sql_fields ="WHERE `Status_ID` = '$status' AND `Product` = '$product'";
}
else {
$sql_fields ="WHERE `Product` = '$product'";
}
$b++;
}
if (isset($_GET['company'])&& $_GET['company'] >0){
$company = $_GET['company'];
if($a == 0 && $b == 0){
$sql_fields ="WHERE `Company_ID` = '$company'";
}
else if($a == 0 && $b == 1){
$sql_fields ="WHERE `Product` = '$product' AND `Company_ID` = '$company'";
}
else if($a == 1 && $b == 0){
$sql_fields ="WHERE `Status_ID` = '$status' AND `Company_ID` = '$company'";
}
else {
$sql_fields ="WHERE `Status_ID` = '$status' AND `Product` = '$product' AND `Company_ID` = '$company' ";
}
$c++;
}
if ($a == 0 && $b == 0 && $c == 0){
$sql_fields =" ";
}
if (isset($_GET['date_from']) && $_GET['date_from'] >0 && isset($_GET['date_to'])&& $_GET['date_to'] >0){
if ($access_level == 1){
if ($a == 0 && $b == 0 && $c == 0){
$search_date = "WHERE `Date_added` >= '$date_from' AND `Date_added` <= '$date_to'";
}
else {
$search_date = "AND `Date_added` >= '$date_from' AND `Date_added` <= '$date_to'";
}
}
if ($access_level ==2 or $access_level ==3){
if ($a == 0 && $b == 0 && $c == 0){
$search_date = " `Date_added` >= '$date_from' AND `Date_added` <= '$date_to'";
$and = "AND";
}
else {
$search_date = " `Date_added` >= '$date_from' AND `Date_added` <= '$date_to'";
$and = "AND";
}
}
}
else {
$search_date = "";
$and = "";
}
这是我修改后的代码!
$fields = array();
if (isset($_GET['status']) && $_GET['status'] >0){
$status = $_GET['status'];
$fields['Status_ID']= $_GET['status'];
}
if (isset($_GET['product']) && $_GET['product'] >0){
$product = $_GET['product'];
$fields['Product_ID']= $_GET['product'];
}
if (isset( $_GET['company']) && $_GET['company'] >0){
$company = $_GET['company'];
$fields['Company_ID']= $_GET['company'];
}
if (isset( $_GET['closer']) && $_GET['closer'] >0){
$closer = $_GET['closer'];
$fields['Closer']= $_GET['closer'];
}
if (isset( $_GET['date_from']) && $_GET['date_from'] >0 && isset( $_GET['date_to']) && $_GET['product'] >0){
$date_from = $_GET['date_from'];
$date_from = $_GET['date_to'];
}
$field_count = count($fields);
if ($field_count == 0){
$sql = "";
}
else {
$field_count --;
$sql="";
$i=0;
foreach($fields as $k => $v) {
if($i==0){
$sql = "WHERE `$k` = '$v'";
}
else{
$sql .=" AND `$k` = '$v'";
}
$i++;
}
}
echo $sql;
最佳答案
now i have used lots of nested divs to work out if the gets are set
在一个脚本中混合 html、php 和 sql 是一种久经考验的意大利面条代码配方。如果您至少不想完全分离 HTML 和 PHP
- 将 PHP 逻辑置于您的脚本之上
- 清理所有给定的输入
- 建立where条件
- 查询数据库
然后仅通过在适当的位置输出数据库结果来呈现 HTML 部分。不要将上述任何逻辑放入脚本的 HTML 部分。
至于构建 where 子句的条件逻辑:我不知道它的作用。我看了它 30 秒,但代码格式很差,并且有 $a、$b 和 $c 等带有魔数(Magic Number)的变量。您应该选择具有适当缩进和格式的一致编码风格。你应该适本地命名你的变量,并使用常量或函数调用最小化魔数(Magic Number)。从长远来看,这将使代码更具可读性。而且您(和其他开发人员)将能够更轻松地了解正在发生的事情。
关于php - 如何在 SQL 语句中添加 QBE 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8414951/