1

MyController:

class MY_Controller extends CI_Controller {

        public $data = array();

        function __construct() {
            parent::__construct();
            $this->data['errors'] = array();
            $this->data['site_name'] = config_item('site_name');
        }

}

AdminController:

class Admin_Controller extends MY_Controller {

    function __construct() {
        parent::__construct ();
        $this->data ['meta_title'] = 'Admin Panel';
        $this->load->helper ( 'form' );
        $this->load->library ( 'form_validation' );
    }
}

UserController:

class User extends Admin_Controller {

        public function __construct() {
            parent::__construct();
        }

        public function login() {
            $this->data['subview'] = 'admin/user/login';
            $this->load->view('admin/_layout_modal', $this->data);
        }
         }

View: _layout_modal.php

$this->load->view($subview);
echo $meta_title;

But both $subview and $meta_title are throwing "Undefined variable" error.

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: subview

Filename: admin/_layout_modal.php

2
  • any suggestions at all? I'm breaking my head from 24hrs to fix this Commented Jan 19, 2014 at 9:35
  • Where is your adminController placed? Commented Jan 19, 2014 at 11:12

2 Answers 2

1

In default, $this->load->view send the data to browser directly.

It seems you want to load view as string format, not sending to browser directly.

You need to add one parameter to the 'view' function

I think you should change your codes like this:

UserController:

    public function login() {
        $this->data['subview'] = $this->load->view('admin/user/login', true);
        $this->load->view('admin/_layout_modal', $this->data);
    }

View: _layout_modal.php

echo $subview;
echo $meta_title;

Reference:

http://ellislab.com/codeigniter/user-guide/general/views.html

Sign up to request clarification or add additional context in comments.

2 Comments

OP's storing the name of the view inside the $subview variable, and loading the child view inside the main view using that name . While your answer is technically right, also is OP's code
Yeah! His approach is also right. There are many ways, that's cool. But in the view page I'm stuck at Undefined variable error, even though I've added $this->data in the controller. I shoud'nt be getting that error. Somehow this line is not doing it's job right: $this->load->view('admin/_layout_modal', $this->data);
0

try to use $data['subview'] not $this->data['subview'] and when loading the view you use $this->load->view('admin/layout',$data); and in layout file call <?php echo $subview; ?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.