Welcome Guest, Not a member yet? Register   Sign In
Exit on if-statement inside controller function?
#1

[eluser]sorenchr[/eluser]
Hi there! Im new to CodeIgniter, so far it's a thrill to use, however I can't seem to figure out how to exit on an if-statement inside a controller function. I have this code:

Code:
function auth() {
        //If no submit post-data is send, give error
        if(!$this->input->post('username') || !$this->input->post('password')) {
            $data['error'] = "true";
            $this->output->set_output($this->load->view('login_view', $data));
        }
        
        //$this->load->model('gatekeeper');
        $query = "true"; // $this->gatekeeper->auth();
        if($query) {
            $data['error'] = "false";
            $this->output->set_output($this->load->view('login_view', $data));
        } else {
            $data['error'] = "true";
            $this->output->set_output($this->load->view('login_view', $data));
        }

    }

The $query = "true" bit is just there to test the code. Well, the page goes blank, which it shouldn't, it should either send $error = true or false on to the login_view. How do I do that?

Thanks for your time!
#2

[eluser]sorenchr[/eluser]
Perhaps I should clarify a bit, what I'm essentially trying to do here is this:
Code:
function auth() {
        //If no submit post-data is send, give error
        if(!$this->input->post('username') || !$this->input->post('password')) {
            $data['error'] = "true";
            $this->load->view('login_view', $data);
            exit;
        }
        
        //$this->load->model('gatekeeper');
        $query = "true"; // $this->gatekeeper->auth();
        if($query) {
            $data['error'] = "false";
            $this->load->view('login_view', $data);
            exit;
        } else {
            $data['error'] = "true";
            $this->load->view('login_view', $data);
            exit;
        }

    }
#3

[eluser]InsiteFX[/eluser]
Why are you using exit?

The if then else statement will only do one or the other

if (condition == true)
{
do this if true
}
else
{
do this if false
}

It will only do one or the other not both...

Enjoy
InsiteFX
#4

[eluser]jplanet[/eluser]
Exit would only be called in a switch case. I'm also wondering why the same view is loaded with every condition. Why not just load the view once after the conditions set the $data['error'] variable?
#5

[eluser]sorenchr[/eluser]
Okay, I guess I could make it something like InsiteFX suggested. (Thanks btw)
This raises a bonus question however, what if I want want to validate some data with a bunch of models and different methods inside of my controller? Wouldn't that create an ugly piece of code nested inside of 3000 if-statements?




Theme © iAndrew 2016 - Forum software by © MyBB