Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a view based on condition in Controller (Codeigniter)

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

I'm trying to determine the site settings if enabled (1) or not (0) and display the index view (if site value == 1)

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.

Model

function getsetting() {
$this->db->select("site");
$this->db->from('configuration');
$query = $this->db->get();
$ret = $query->row();
return $ret->site;
}

Controller

    //if site value is == 1 load the normal view
    $this->load->view('index', $data);
    //else load the maintenance view
    $this->load->view('maintenance', $data)

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.

like image 577
Brian Luna Avatar asked May 19 '26 16:05

Brian Luna


1 Answers

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]

Controller

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)

        }

    }
like image 60
Saty Avatar answered May 21 '26 06:05

Saty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!