如何基于Controller(Codeigniter)中的条件加载视图 [英] How to load a view based on condition in Controller (Codeigniter)
问题描述
我是Codeigniter的新手。我正在尝试制作一个简单的网站,如果该网站处于启用状态(1)或未启用(0),则将在数据库上获取其设置。
I'm new to codeigniter. I'm trying to make a simple site, that will get its "settings" on the database, if the site is enabled (1) or not (0).
我正在尝试确定网站设置(如果已启用(1)或未启用(0)
)并显示索引视图(如果网站价值== 1)
I'm trying to determine the site settings if enabled (1) or not (0) and display the index view (if site value == 1)
和维护视图(如果站点值为== 0或为空)
and maintenance view (if site value is == 0 or null)
我已经编写了代码,可以回显站点的价值。
I have written the code already and I can echo the "site" value in a view.
模型
function getsetting() {
$this->db->select("site");
$this->db->from('configuration');
$query = $this->db->get();
$ret = $query->row();
return $ret->site;
}
控制器
//if site value is == 1 load the normal view
$this->load->view('index', $data);
//else load the maintenance view
$this->load->view('maintenance', $data)
我有一个数据库 mysite,其中有一个表 configuration和一个列 site,值[null或1]来自site列,用于确定应显示哪个视图。
I have a database "mysite" with one table "configuration" and one column "site" the values [null or 1] are from site column, for determining what view should be displayed.
可以接受任何帮助。
Any help is accepted. Thanks in advance.
推荐答案
模型
在这里,您需要使用来获取单行 $ query-> row();
并将其传递给控制器
MODEL
Here you need to fetch single row by using $query->row();
and pass it to controller
function getsetting() {
$this->db->select("site");
$this->db->from('configuration');
$query = $this->db->get();
$ret = $query->row();
return $ret->site;
}
正如您在问题中所述
As you describe in your question
the values [null or 1]
控制器
function your_controler() {
$this->load->model('model_file');
$site = $this->model_file->getsetting();
if(isset($site) && $site==1){// your condition here
$this->load->view('index', $data);
}else{
$this->load->view('maintenance', $data)
}
}
这篇关于如何基于Controller(Codeigniter)中的条件加载视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!