Search code examples
phpmodel-view-controllercodeigniter-2

MVC CodeIgniter, trouble getting array to pass to view


The idea is I am checking the database for values dependant on wether the fields are null depends on the state of the button, get_login_state() works correctly with the desired result, however get_break_state() does not.

In my controller in the if($resultTwo){} The code in here never executes but resultTwo collects the result and isn;t null so I don;t understand why.

Here is my controller:

    $resultOne = $this->dashboard_model->get_login_state($user_id);

    //die(print_r($resultOne));

    $resultTwo = $this->dashboard_model->get_break_state($user_id, $this->session->userdata('user_session'));

    //die(print_r($resultTwo));

    $this->login_state = array();

    if($resultOne){
        $this->login_state['resultWork'] = $resultOne;
        $this->template->content = View::factory('dashboard', $this->login_state);
    } else {
        return $this->normal['status'] = false; 
    }

    //die(print_r($resultTwo));

    if($resultTwo) {
        //die('two');
        $this->login_state['resultBreak'] = $resultTwo;
        $this->template->content = View::factory('dashboard', $this->login_state);
    } else {
        return $this->normal['status'] = false;
    }

Here is my model:

public function get_login_state($user_id){

    $this->db->select('loggedin_date, loggedout_date');
    $this->db->where('user_id = '.$user_id);
    $this->db->where('user_session = "'.$this->session->userdata('user_session').'"');      
    $select = $this->db->get('clock_in_out');

    //die($this->db->last_query());

    if($select){
        return $select->row();
    } else {
        return false;
    }

}

public function get_break_state($user_id, $session){

    $this->db->select('in_date, out_date');
    $this->db->where('user_id = '.$user_id);
    $this->db->where('session = "'.$session.'"');
    $this->db->order_by('in_date DESC');
    $select = $this->db->get('breaks');

    //die($this->db->last_query());

    if($select){
        return $select->row();
    } else {
        return false;
    }
}

Here is what I have to manipulate the objects in the view:

if(isset($resultWork)){
    if($resultWork->loggedin_date){
        $startWorkBtnState = 'disabled';
    }
    if($resultWork->loggedout_date){
        $endWorkBtnState =  'disabled';
    }
}
if(isset($resultBreak)){
    if($resultBreak->in_date){
        $startBreakBtnState = 'disabled';
    }
if($resultBreak->out_date){
    $endBreakBtnState = 'disabled';
}

}


Solution

  • OK think I have got you..

    If your first controller case if($resultOne){} evaluates to true then you are loading the view without setting $this->login_state['resultBreak'] to anything. You need to be smarter with your if's and improve your logic. For a quick fix initialise your variables first to avoid the notice

    $this->login_state['resultWork'] = null;
    $this->login_state['resultBreak'] = null;