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条)

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”