如何根据控制器中的条件加载视图[codeigniter] [英] How to load a view based on condition in controller [codeigniter]

查看:102
本文介绍了如何根据控制器中的条件加载视图[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)

和maintenace视图(如果网站值是== 0或null)

and maintenace 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有一个表配置和一列网站值[null或1]是从网站列,用于确定应显示哪个视图

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.

推荐答案

MODEL
这里需要使用 $ 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)

        }

    }

这篇关于如何根据控制器中的条件加载视图[codeigniter]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆