How can you load a view in CodeIgniter?

The View can’t be accessed directly. It is always loaded in the controller file. Following function is used to load a view page:

$this->load->view(‘page_name’);
Write your view’s page name in the bracket. You don’t need to specify .php unless you are using some other extension.

In CodeIgniter, you can load a view using the load->view() method. This method is typically used within a controller to render a view and pass data to it if needed. Here’s how you would use it:

php
$this->load->view('view_name', $data);

Where:

  • 'view_name' is the name of the view file you want to load. It should be located in the application/views/ directory.
  • $data is an optional parameter containing an associative array of data that you want to pass to the view.

For example, if you want to load a view named “welcome_message.php” located in the application/views/ directory, you would use:

php
$this->load->view('welcome_message');

If you want to pass data to the view, you can do so by passing an associative array as the second parameter:

php
$data = array(
'title' => 'Welcome to my website',
'content' => 'This is the content of my webpage'
);

$this->load->view('welcome_message', $data);

Inside the view file (welcome_message.php in this case), you can access the data passed using $title, $content, etc.