在PHP中重构if语句或针对if语句的其他解决方案(不是开关大小写)

在PHP中重构if语句或针对if语句的其他解决方案(不是开关大小写),php,mysql,refactoring,Php,Mysql,Refactoring,我的代码中有一些if语句。 e、 g: 上述代码的一位伪代码: 如果foo=1和bar=1->返回foo和bar 如果foo=0且bar=1->仅返回bar 如果foo=1且bar=0->仅返回foo 如果foo=0且bar=0->返回false 你看, 00 十, 01 十一, 00 如果我插入另一个变量,我会得到更多的机会,这真的很糟糕。因为我会得到非常大的if语句 有人能告诉我另一个机会来达到同样的结果吗 谢谢。请尝试以下代码: $sub_query = $operator = '';

我的代码中有一些if语句。 e、 g:

上述代码的一位伪代码:

如果foo=1和bar=1->返回foo和bar

如果foo=0且bar=1->仅返回bar

如果foo=1且bar=0->仅返回foo

如果foo=0且bar=0->返回false

你看,

00

十,

01

十一,

00

如果我插入另一个变量,我会得到更多的机会,这真的很糟糕。因为我会得到非常大的if语句

有人能告诉我另一个机会来达到同样的结果吗

谢谢。

请尝试以下代码:

$sub_query = $operator = '';

if($option[0]->posts == 1)
{
    $sub_query = 'post_type="page"';
    $operator = ' OR';
}
if($option[0]->pages == 1)
{
    $sub_query .= $operator.' post_type="post"';
}

if(empty($sub_query))
{
    return false;
}
else
{
$results = $wpdb->get_results( 'SELECT * FROM '.$wpdb->prefix.'posts WHERE post_status="publish" AND ('.$sub_query.') ORDER BY post_title ASC', OBJECT );   
}     
我会这样做:

$sql_condition = '( 1=2 '; // one fake-condition, just to make it possible to start with 'OR' later

foreach($option[0] as $key => $value) {  // iterate through all possible conditions
    if($value===1) { // maybe exclude $keys that should not be used here
        $sql_condition.=' OR post_type="'.$key.'"';
    }
}
$sql_condition.=')';

$results = $wpdb->get_results( 'SELECT * FROM '.$wpdb->prefix.'posts WHERE post_status="publish" AND '.$sql_condition.' ORDER BY post_title ASC', OBJECT );
$tempKey = $option[0]->pages . "," . $option[0]->posts;
if(isset($arr[$tempKey]) {
     $results = $wpdb->get_results($arr[$tempKey]); 
}
创建一个数组($arr),并将键设置为“0,0”,值设置为“$sql”; 您的代码如下所示:

$sql_condition = '( 1=2 '; // one fake-condition, just to make it possible to start with 'OR' later

foreach($option[0] as $key => $value) {  // iterate through all possible conditions
    if($value===1) { // maybe exclude $keys that should not be used here
        $sql_condition.=' OR post_type="'.$key.'"';
    }
}
$sql_condition.=')';

$results = $wpdb->get_results( 'SELECT * FROM '.$wpdb->prefix.'posts WHERE post_status="publish" AND '.$sql_condition.' ORDER BY post_title ASC', OBJECT );
$tempKey = $option[0]->pages . "," . $option[0]->posts;
if(isset($arr[$tempKey]) {
     $results = $wpdb->get_results($arr[$tempKey]); 
}

因此,当您想添加更多页面和帖子时,您所要做的就是更改arr。

对我不起作用$在我的例子中,sql_条件为空。我必须更改某些内容,但它现在可以工作了。谢谢。您能告诉我您必须更改哪些内容,以便我能将其包含在答案中吗?如果($value==1&&&$key!='id'&&&&$key!='searchbox'){$sql\u条件='或post\u type=“'.$key.'”;}我会试试。但这就是我的问题:“where子句”中的未知列“post”您需要修复与
quote
s相关的查询中的错误。如果类型增加,则需要添加更多
If
条件。如果我插入另一个变量,我将获得更多的机会$option[0]->posts和$option[0]->页面只有两个值,只有1或0。是的。但我需要一个脚本,可以处理001,010,100。。。或者可能是000100100100。。。。
$types = [];
if ($option[0]->posts)
    $types[] = '"post"';
if ($option[0]->pages)
    $types[] = '"page"';
if (!$types)
    return null;
$results = $wpdb->get_results( 'SELECT * FROM '.$wpdb->prefix.'posts WHERE post_status="publish" AND (post_type IN ('. implode(',', $types) .')) ORDER BY post_title ASC', OBJECT );