【问题标题】:PHP Codeigniter If else value always result in nullPHP Codeigniter If else 值总是导致 null
【发布时间】:2018-03-31 03:51:46
【问题描述】:

您好,我正在开发一个事件管理站点,我在控制器上使用 if else 语句来导航 2 个视图页面(一个带有文件附件的视图页面和一个没有文件附件的视图页面)我的问题是每次执行 if来自我的控制器的 else 语句我总是以数据库上没有附加文件的情​​况结束(空)

这是我的控制器:

public function viewevent(){

$id = $this->uri->segment(3);

$data['events'] = $this->EventModel->getEventII($id);

$check = $this-$this->EventModel->getEventII($id);   

if($check->event_file == null){

$this->load->view('include/schoolrep/toppage', $data);
$this->load->view('include/schoolrep/header', $data);
$this->load->view('schoolrep/viewevent2', $data); // loads page where there is no file attached
$this->load->view('include/schoolrep/footer', $data);
}else{

$this->load->view('include/schoolrep/toppage', $data);
$this->load->view('include/schoolrep/header', $data);
$this->load->view('schoolrep/viewevent', $data); //loads page where there is file attached
$this->load->view('include/schoolrep/footer', $data);
}

}

这是我的模型

public function getEventII($id){
  $q = $this->db->get_where('tblevents',array('event_id'=>$id));
  return $q->row();

}

正如您在我的 if else 语句中看到的那样,我正在从数据库中检查我的 event_file 列是否为空,但我总是最终加载没有附加文件或 (event_file == null) 的视图文件,即使有。

谢谢。

【问题讨论】:

  • 试试is_null($check->event_file)$check->event_file === null
  • @ArtisticPhoenix ok 试试看谢谢
  • @ArtisticPhoenix 问题依旧
  • 你能检查一下 $check 变量给出了什么吗?
  • 我以为会的,只是null == false == 0 == '' == []等等...

标签: php codeigniter if-statement null


【解决方案1】:

您可以将this 行更改为

 $check =  $this-$this->EventModel->getEventII($id); 

这个

$check =  $this->EventModel->getEventII($id); 

【讨论】:

  • 是的这个!非常感谢,我刚刚想通了,也感谢队友
  • 是的,我正要这样做,但该网站说我可以在 1 分钟后接受您的回答,但其完成的回答已接受,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多