douxiong2999 2019-08-18 18:31
浏览 127
已采纳

检查表codeigniter中是否已存在两个值

I.m trying to check if a value exists in two columns from a table. The column names are on_number and off_number.

I've tried the following in my controller. The check works however for only the off_number columns and not the on_number.

my controller.

 public function check()
        {
            $on_number = !empty(get('on_number')) ? get('on_number') : false;
            $notId = !empty($this->input->get('notId')) ? $this->input->get('notId') : 0;
            if($on_number)
                $exists = count($this->duty_book_model->getByWhere([
                        'on_number' => $on__number,
                        'id !=' => $notId,
                    ])) > 0 ? true : false;
                if($on_number)
                $exists = count($this->duty_book_model->getByWhere([
                        'off_number' => $on_number,
                        'id !=' => $notId,
                    ])) > 0 ? true : false;
            echo $exists ? 'false' : 'true';
        }

 My Model 

    class Duty_book_model extends MY_Model {

        public $table = 'tbl_duty_type';

        public function __construct()
        {
            parent::__construct();
        }
    }

The extends MY_Model has:

public function getByWhere($whereArg, $args = [])
    {

        if(isset($args['order']))
            $this->db->order_by($args['order'][0], $args['order'][1]);

        return $this->db->get_where($this->table, $whereArg)->result();
    }

I would like it to check both columns if the value exists.

  • 写回答

2条回答 默认 最新

  • doujiang2812 2019-08-18 19:32
    关注

    I believe the reason it doesn't work for "off_number" is in this code

    if($on_number)
    $exists = count($this->duty_book_model->getByWhere([
              //THE NEXT LINE IS THE PROBLEM - LOOK AT THE VALUE!!!
              'off_number' => $on_number, //value should be $off_number - right?
              'id !=' => $notId,])) > 0 ? true : false;
    

    That said, I think your code is a hot mess. I say that because to adhere to the MVC pattern a lot of your controller logic should be in the model. IMO, all of it should be.

    I would replace the model function getByWhere() with one named check(). It will return true if any record where 'id != $notIdAND'on_number' = $on__number` AND off_number', $off_number. If either of the requred inputs are missing, or if no records are found it returns false.

    As you look at the following code it's important to understand that $this->input->get('some_input') will return null if $_GET('some_input') is empty.

    class Duty_book_model extends MY_Model
    {
        public $table = 'tbl_duty_type';
    
        public function check()
        {
            $on_number = $this->input->get('on_number');
            $notId = $this->input->get('notId');
            if(empty($on_number) || empty($notId))
            {
                return false;
            }
    
            $query = $this->db
                    ->select('id')  //could be any field
                    ->where('id !=', $notId)
                    ->where('on_number', $on__number)
                    ->where('off_number', $off_number) 
                    ->get($this->table);
            //given $notId = 111 AND $on_number = 222 AND $off_number = 333           
            //produces the query string 
            //SELECT `id` FROM `tbl_duty_type` WHERE `id` != 111 AND `on_number` = 222 AND `off_number` = 333
    
            if($query) //might be false if the query string fails to work
            {
                return $query->num_rows > 0; //returns boolean
            }
    
            return false;
        }
    
    }
    

    Then all you need in the controller is

    $exists = $this->duty_book_model->check();
    echo $exists ? 'false' : 'true';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 来一个会抓包app支付接口的
  • ¥30 vivado2017.4的sdk打不开
  • ¥50 matlab,文献复现
  • ¥20 寻找Flink CDC指导,有偿咨询
  • ¥15 cruise如何仿真不同温度下车辆的经济性?
  • ¥20 扑克牌游戏的完整代码
  • ¥88 求帮安装Python架构的vnpy
  • ¥20 逻辑回归法分析品牌知名度和购买意向
  • ¥15 华为云主机-异常连接
  • ¥50 vue 成绩列表 输入成绩,并统计各分数段人数