lo..." /> lo..." /> lo..."/>

How to load a View based on multiple conditions in Controller (Codeigniter)

1.8k Views Asked by At

I'm new to codeigniter. I'm trying to make a simple site, with three (3) conditional views.

For example:
If "user_agent" detects mobile device -> load mobile_view
else load -> web_view
and if "site" settings is disabled [value = 0] load -> maintenance_view


I have the following code but doesn't work. It always load the maintenance view.

Controller:

function index() {
        $this->load->library('user_agent');
        if($this->agent->is_mobile())
        {
            $this->load_mobile();   
        } else {
            $this->load_web();
        }
    }

    public function load_web() {
       $site = $this->Datamodel->getsetting();
       if(isset($site) && $site==1) { //check if site settings is enabled [(if site "value == 1" load -> web_view) ELSE (site "value == null" load -> maintenace_view)]
            $this->load->view('web_view');
        } else {
            $this->load->view('maintenance_view');
        }
    }

    public function load_mobile() {
        $this->load->view('mobile_view');
    } 


Model for site settings:

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

For site settings conditions, I have extendted my old code from here How to load a view based on condition in Controller (Codeigniter)

The previous code only have 2 conditions if site is enabled [site value == 1] or not [site value == null]

Depending on conditions, web or index view is loaded in controller if site "value == 1" otherwise if site "value == null" load the maintenace view


Now Im trying to add a mobile view, but I cant figure it out how, with the rest of the code.

1

There are 1 best solutions below

0
On BEST ANSWER
function index() {
if(site_value == 0){
 $this->load->view('maintenance_view');
      }else{
            $this->load->library('user_agent');
            if($this->agent->is_mobile())
            {
                $this->load_mobile();   
            } else {
                $this->load_web();
            }
          }
         }