droc60607 2014-09-11 07:12
浏览 64
已采纳

PHP嵌套ifs和许多条件不起作用

I have many nested ifs and conditions in my code but it is not giving the desired output. What is the best way to write the following code :

$driver_code = $this->input->post('filter_driver_code');

        $unit_code = $this->input->post('filter_unit_code');

        $fuel_type = $this->input->post('filter_fuel');

        $date_to = $this->input->post('date_to');

        $date_from = $this->input->post('date_from');





        if (isset($date_from) and isset($date_to) and empty($unit_code) and empty($driver_code) and empty($fuel_type)) {

            $sql = "SELECT * FROM fuel_usage where (date between '$date_from' and '$date_to') ";
            $result = $this->db->query($sql);
        } elseif (isset($driver_code) and isset($unit_code) and isset($fuel_type) and isset($date_from) and isset($date_to)) {

            $sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type' and (date between '$date_from' and '$date_to')";
            $result = $this->db->query($sql);
        }  elseif (empty ($date_from) and empty ($date_to) and isset ($unit_code) and isset ($driver_code) and isset ($fuel_type)) {

            $sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type'";
            $result = $this->db->query($sql);
        }  else {
            $sql="SELECT * FROM FUEL_USAGE";
            $result = $this->db->query($sql);
        }

It does not give the right output.

  • 写回答

2条回答 默认 最新

  • dqdz6464 2014-09-11 07:19
    关注

    Try all check with empty() may be isset() gives you problem cause on post isset for all variable will return true even values blank

           if (!empty($driver_code) && !empty($unit_code) && !empty($fuel_type) && !empty($date_from) && !empty($date_to)) {
                $sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type' and (date between '$date_from' and '$date_to')";
                $result = $this->db->query($sql);
            }elseif (!empty($date_from) && !empty($date_to) && empty($unit_code) && empty($driver_code) && empty($fuel_type)) {
                $sql = "SELECT * FROM fuel_usage where (date between '$date_from' and '$date_to') ";
                $result = $this->db->query($sql);
            }  elseif (empty($date_from) && empty($date_to) && !empty($unit_code) && !empty($driver_code) && !empty($fuel_type)) {
                $sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type'";
                $result = $this->db->query($sql);
            }  else {
                $sql="SELECT * FROM FUEL_USAGE";
                $result = $this->db->query($sql);
            }
    

    or try in short way

    $sql = "SELECT * FROM fuel_usage where 1 ";
    if (!empty($driver_code)) {
      $sql .= " AND driver_code='$driver_code' ";
    } 
    if (!empty($unit_code)) {
      $sql .= " AND unit_code='$unit_code' ";
    } 
    if (!empty($fuel_type)) {
      $sql .= " AND fuel_type='$fuel_type' ";
    } 
    if (!empty($date_from) && !empty($date_to)) {
      $sql .= " AND date between '$date_from' and '$date_to' ";
    }
    $result = $this->db->query($sql); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵
  • ¥15 cfx离心泵非稳态计算