简体   繁体   中英

using switch case for loading pages in codeigniter

I learned Codeigniter basics but i then stopped. So im relearning.

I know there are more advanced ways of doing this but i'd like to do things my way in which i can understand. I don't want to bite off advanced stuff that will only confuse me. Copy pasting snippets online is very easy but i want to understand everything in my code.

Anyways, i've always used if statements back in php, idk why, i never really looked up proper guidelines in coding so in some cases where a switch case was best i was still using the familiar if statements.

So in my project, my links are made this way.

<a href="<?php echo base_url();?>pages/view/home">Homepage</a>

And in my controller, i have set up my view method to take out the uri-segment to know which pages to load.

$data = array('page' => $this->uri->segment(3,home));

switch ($data['page']){
case 'home';
$data['tile'] = "Homepage";
$data['page'] = "userhome";
break;
case 'login';
$data['tile'] = "Login";
$data['page'] = "userlogin";
break
}

$this->load->view('templates/header',$data);
$this->load->view('templates/navbar');
$this->load->view('pages/'.$data['page']);
$this->load->view('templates/footer');

But it doesn't work. The title and page won't change.

I can easily do this with if else statement.

$temp = $this->uri->segment(3,'home');

if($temp == "home"){
$data = array('title' => 'Homepage' , 'page' => 'home');
$this->load->view('template/header',$data);
$this->load->view('template/navbar');
$this->load->view('pages/'.$data['page']);
$this->load->view('template/footer');
}else if($temp == "login"){
$data = array('title' => 'Login' , 'page' => 'userlogin');
$this->load->view('template/header',$data);
$this->load->view('template/navbar');
$this->load->view('pages/'.$data['page']);
$this->load->view('template/footer');   
}

I read somewhere that switch case is faster than an if else, i'm just showing you the first 2 pages ive made so far, login and homepage but any other pages ill make in the future will be loaded in the same way so the code will get longer. that's why i was interested in using switch case.

This worked though

$temp = $this->uri->segment(3,'home');      
        switch ($temp){
        case 'home':
        $data = array('title' => "Homepager" , 'page' => "home");
        $this->load->view('template/header',$data);
        $this->load->view('template/navbar');
        $this->load->view('pages/'.$data['page']);
        $this->load->view('template/footer');   
        break;
        case 'login':
        $data = array('title' => "Login" , 'page' => "userlogin");
        $this->load->view('template/header',$data);
        $this->load->view('template/navbar');
        $this->load->view('pages/'.$data['page']);
        $this->load->view('template/footer');   
        break;
        }

可能是因为switch语句中的类型错误,您正在为data['tile']而不是data['title']分配值吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM