这样的ifelse循环怎么优化好?????

咸菜. 2018-01-29 02:11:08
if ($openId == '' && $billno == '' && $status == '0') {
$where = "`gameId` = '$gameId' and `status` = '$status'";
} else if ($openId == '' && $billno == '' && $status == '1') {
$where = "`gameId` = '$gameId' and `status` <> '0'";
} else if ($openId != '' && $billno != '' && $status == '0') {
$where = "`gameId` = '$gameId' and `status` = '$status'and billno = '$billno' and openId= '$openId'";
} else if ($openId == '' && $billno != '' && $status == '0') {
$where = "`gameId` = '$gameId' and `status` = '$status'and billno = '$billno'";
} else if ($openId == '' && $billno != '' && $status == '1') {
$where = "`gameId` = '$gameId' and `status` <> '0' and billno = '$billno'";
} else if ($openId != '' && $billno == '' && $status == '1') {
$where = "`gameId` = '$gameId' and `status` <> '0' and openId = '$openId'";
} else if ($openId != '' && $billno == '' && $status == '0') {
$where = "`gameId` = '$gameId' and `status` = '$status'and openId = '$openId'";
} else {
$where = "`gameId` = '$gameId' and `status` <> '0'and billno = '$billno' and openId= '$openId'";
}
$sql = "SELECT * FROM `codes_buy_records` WHERE $where and time BETWEEN '$starttime' and '$endtime' order by time desc";
...全文
245 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
木乄木 2018-01-29
  • 打赏
  • 举报
回复
引用 楼主 qq_34479038 的回复:
if ($openId == '' && $billno == '' && $status == '0') {
            $where = "`gameId` = '$gameId' and `status` = '$status'";
        } else if ($openId == '' && $billno == '' && $status == '1') {
            $where = "`gameId` = '$gameId' and `status` <> '0'";
        } else if ($openId != '' && $billno != '' && $status == '0') {
            $where = "`gameId` = '$gameId' and `status` = '$status'and  billno = '$billno' and openId= '$openId'";
        } else if ($openId == '' && $billno != '' && $status == '0') {
            $where = "`gameId` = '$gameId' and `status` = '$status'and  billno = '$billno'";
        } else if ($openId == '' && $billno != '' && $status == '1') {
            $where = "`gameId` = '$gameId' and `status` <> '0' and  billno = '$billno'";
        } else if ($openId != '' && $billno == '' && $status == '1') {
            $where = "`gameId` = '$gameId' and `status` <> '0' and  openId = '$openId'";
        } else if ($openId != '' && $billno == '' && $status == '0') {
            $where = "`gameId` = '$gameId' and `status` = '$status'and  openId = '$openId'";
        } else {
            $where = "`gameId` = '$gameId' and `status` <> '0'and  billno = '$billno' and openId= '$openId'";
        }
        $sql = "SELECT * FROM `codes_buy_records` WHERE $where and time BETWEEN '$starttime' and '$endtime' order by time desc";
完整版
$where = ' `gameId` = "'.$gameId.'"';
if ($status) {
    $where  .= ' AND `status` <> 0';
}else{
    $where .= ' AND `status` = "'.$status.'"';
}

if ($openId) {
    $where .= ' AND `openId` = "'.$openId.'"'; 
}
if ($billno) {
    $where .= 'AND `billno` = "'.$billno.'"';
}
咸菜. 2018-01-29
  • 打赏
  • 举报
回复
引用 2 楼 m0_38030271 的回复:
大概是这个意思 你需要在中间 加上 and 并且把 gameid拼上
我明白了,太感谢啦
木乄木 2018-01-29
  • 打赏
  • 举报
回复
大概是这个意思 你需要在中间 加上 and 并且把 gameid拼上
木乄木 2018-01-29
  • 打赏
  • 举报
回复
$where = '';
if ($status) {
    $where  .= '`status` <> 0';
}else{
    $where .= '`status` = "'.$status.'"';
}

if ($openId) {
    $where .= '`openId` = "'.$openId.'"'; 
}
if ($billno) {
    $where .= '`billno` = "'.$billno.'"';
}

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧