Welcome Guest, Not a member yet? Register   Sign In
Controller, view question...
#1

[eluser]mrh[/eluser]
Hello all,

Please forgive if this has been asked (probably many times)... I've searched and not seen the answer to this very simple and probably idiotic question... I do understand MVC and I've been using OOP for many years but I am new to PHP and CI... My question is this:

For each view, I have a controller. for example:

adminlogin - controller
adminlogin_view - view

adminportal - controller
adminportal_view - view

I also have a data model for getting stuff from the database. My question is this:

In my adminlogin controller index() function I load the adminlogin_view. This displays a username/password field. The submit button is set to call adminlogin->submit(). In this function I see if the username and password are correct using a function from the model. If they are correct I want to send them off to the adminportal view where they have functions to update/edit and do admin type stuff.

So in submit() I am doing the following if they validate ok (correct username/password)

Code:
$name = $this->input->post('userName');
$pass = $this->input->post('userPass');
$adminID = $this->data_model->checkAdminlogin( $name, $pass );
  
if( $adminID>0 )
{
// This session data is vital to allow the admin_portal to function
$this->session->set_userdata( 'axID', $adminID );
  
// Count applications that exist
$data = array();  
    
// Get application data
$appCount = 0;
$appData = array();
$this->data_model->getApplicationsData( $appCount, $appData );
          
$data['appCount'] = $appCount;
$data['appData'] = $appData;
        
// Setup menus for this view - No highlight
$menuData['menus'] = self::constructMenu( "" );
  
// Load the view
$this->load->view( 'header', $menuData );
$this->load->view( 'adminportal_view', $data );
$this->load->view( 'footer', $menuData );
}

This is all well and good but it is a duplicate of the data gathering code in the index() function of the adminportal controller! IE I have to have this code in two places! First I need to have it here so that when I vector the user to the adminportal the data is loaded. I also have to have this identical code in the index() function of the adminportal because after they do a particular function and they want to come back to the portal I need to load the same data.

I guess in simple terms I'm wondering what I'm doing wrong. When I execute the lines:

Code:
// Load the view
$this->load->view( 'header', $menuData );
$this->load->view( 'adminportal_view', $data );
$this->load->view( 'footer', $menuData );

It seems that the adminportal controller is not loaded nor is index called. This kind of makes sense because the current controller executing is "adminlogin". What I think I want to do is to let the current adminlogin controller quit and give control the the adminportal controller so it can load the correct data and move forward.

Hopefully I'm just being stupid here and totally missing something but I've been doing this based on the advice in the myriad tutorials. It seems to me that what I really want to do and I have no idea how to do it is instead of just loading the "adminportal_view" from the current controller is to load the adminportal controller and call index().

Logically this would keep the data gathering code grouped with the controller and view.

What am I missing? Is there a way instead of just loading the view to effectively say:

Code:
load_controller( adminportal );

and have that pretty much act as if they called it directly in the url as in www.mysite.com/index.php/adminportal?

Again I apologize for this question. I've probably just missed something and I have been reading the UG and searching about. Sometimes though asking the experts is the only way.



Messages In This Thread
Controller, view question... - by El Forum - 07-30-2012, 04:32 PM
Controller, view question... - by El Forum - 07-30-2012, 05:44 PM
Controller, view question... - by El Forum - 07-30-2012, 06:01 PM
Controller, view question... - by El Forum - 07-30-2012, 07:01 PM
Controller, view question... - by El Forum - 07-30-2012, 07:05 PM
Controller, view question... - by El Forum - 07-30-2012, 08:20 PM



Theme © iAndrew 2016 - Forum software by © MyBB