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

Php 检查表codeigniter中是否已经存在两个值,php,codeigniter,Php,Codeigniter,我试图检查表中的两列中是否存在值。列名为on_编号和off_编号 我在控制器中尝试了以下操作。但是,该检查仅适用于off_编号列,而不适用于on_编号 我的控制器 public function check() { $on_number = !empty(get('on_number')) ? get('on_number') : false; $notId = !empty($this->input->get('no

我试图检查表中的两列中是否存在值。列名为on_编号和off_编号

我在控制器中尝试了以下操作。但是,该检查仅适用于off_编号列,而不适用于on_编号

我的控制器

 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();
        }
    }
扩展MY_模型有:

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();
    }

如果值存在,我希望它检查两列。

首先,您输入了一个输入错误
'on\u number'=>$on\u number,
您在编号上的
$中的下划线增加了一倍

第二件事-您的检查仅适用于
off\u number
的原因是,您在这两种情况下都使用了
$exist
变量。对于编号为on的
的检查结果是什么并不重要,因为它总是会被编号为off的
检查重写

解决这些问题的一种方法是使用两个不同的变量:

if($on_number){
   $exists_on = count($this->duty_book_model->getByWhere([
      'on_number' => $on_number,
      'id !=' => $notId,
   ])) > 0 ? true : false;

   $exists_off = count($this->duty_book_model->getByWhere([
      'off_number' => $on_number,
      'id !=' => $notId,
   ])) > 0 ? true : false;
}
echo (($exists_on===true)&&(exists_off===true)) ? 'false' : 'true';

这不是最好的解决方案,但必须是最清楚的。

首先,你有一个输入错误
'on\u number'=>$on\u number,
您在编号上的
$中的下划线增加了一倍

第二件事-您的检查仅适用于
off\u number
的原因是,您在这两种情况下都使用了
$exist
变量。对于编号为on的
的检查结果是什么并不重要,因为它总是会被编号为off的
检查重写

解决这些问题的一种方法是使用两个不同的变量:

if($on_number){
   $exists_on = count($this->duty_book_model->getByWhere([
      'on_number' => $on_number,
      'id !=' => $notId,
   ])) > 0 ? true : false;

   $exists_off = count($this->duty_book_model->getByWhere([
      'off_number' => $on_number,
      'id !=' => $notId,
   ])) > 0 ? true : false;
}
echo (($exists_on===true)&&(exists_off===true)) ? 'false' : 'true';

这不是最好的解决方案,但必须是最清楚的。

我认为它对“off\u number”不起作用的原因在于此代码

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;
也就是说,我认为你的代码是一团糟。 我说,因为要坚持MVC模式,很多控制器逻辑都应该在模型中。依我看,所有这些都应该是

我会将模型函数
getByWhere()
替换为名为
check()
的函数。如果任何记录的
'id
!=$notId
'on\U number'=$on\U number`和off\U number',$off\U number。如果缺少任何一个请求的输入,或者没有找到任何记录,则返回false

当您查看以下代码时,必须了解,如果
$$this->input->get('some\u input')
为空,则
$\u get('some\u input')
将返回null

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;
    }

}
那么控制器中所需的就是

$exists = $this->duty_book_model->check();
echo $exists ? 'false' : 'true';

我相信它不适用于“off_number”的原因就在这个代码中

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;
也就是说,我认为你的代码是一团糟。 我说,因为要坚持MVC模式,很多控制器逻辑都应该在模型中。依我看,所有这些都应该是

我会将模型函数
getByWhere()
替换为名为
check()
的函数。如果任何记录的
'id
!=$notId
'on\U number'=$on\U number`和off\U number',$off\U number。如果缺少任何一个请求的输入,或者没有找到任何记录,则返回false

当您查看以下代码时,必须了解,如果
$$this->input->get('some\u input')
为空,则
$\u get('some\u input')
将返回null

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;
    }

}
那么控制器中所需的就是

$exists = $this->duty_book_model->check();
echo $exists ? 'false' : 'true';

请显示
duty\u book\u model->getByWhere的代码,我已经修改了我的问题。我希望这有帮助?请显示
duty\u book\u model->getByWhere的代码我已经修改了我的问题。我希望这能帮上忙?嗨,谢谢这也行。我只需要添加以下echo($exists_on==true)| |(exists_off==true))?'假':'真';嗨,谢谢。这也行。我只需要添加以下echo($exists_on==true)| |(exists_off==true))?'假':'真';