How to load a state based view in a controller (Codeigniter)
I am new to codeigniter. I am trying to create a simple site that will get its "settings" in a database if the site is enabled (1) or not (0).
I am trying to detect the site settings if enabled (1) or not (0) and display the index (if site value == 1)
and the type of service (if the site value is 0 or null)
I have already written the code and I can repeat the meaning of "site" in the 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 "Configuration" table and one "site" column with a value of [null or 1] from the site column to determine which view should be displayed.
Any help is appreciated. Thanks in advance.
source to share
MODEL
Here you need to get one line with $query->row();
and pass it to the controller
function getsetting() {
$this->db->select("site");
$this->db->from('configuration');
$query = $this->db->get();
$ret = $query->row();
return $ret->site;
}
As you described 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)
}
}
source to share
Use something like this for your controller:
$setting = $this->modelName->getsetting();
if($setting['maintenance'] == 1){
$this->load->view('maintenance', $data);
} else {
$this->load->view('index' $data);
}
Be sure to replace 'modelName' with the model name of the 'getetting' method.
source to share
If you want to disable all access to a method in maintenance mode, do something like the following in the controller constructor:
public function __construct()
{
parent::__construct();
$site = $this->Datamodel->getsetting();
if(isset($site->site) && $site->site==1)
{
$this->load->view('index');
die;
}
}
source to share