For any queries you can reach us at infovistarindia@gmail.com / WhatsApp us: +919158876092

View in CodeIgniter

Introduction

The View is responsible for the presentation of Model (data) in a certain format. A view is a simple web page created using HTML. We can call a view by using a Controller.

How to create a view

<html>
<head>
	<title>About Us</title>
</head>
<body>
	<h1>Welcome to infovistar.com</h1>
</body>
</html>

Save the file as about_us.php in your application/views directory.

Views

How to load a view

By using $this->load->view() method we can load a view file in a Controller.

For example, $this->load->view('about_us');

The .php file extension does not need to be defined unless you use something other than .php.

Now we can set this view to our about_us method in Welcome.php controller.

						
class Welcome extends CI_Controller {

	public function about_us() {
		$this->load->view('about_us');
	}

}

Add Dynamic Data to the View

We can pass data from the controller to the view by way of an array or an object in the second parameter of the view method.

Here is an example using an array:


$data = array(
        'title' => 'infovistar.com',
        'heading' => 'About Us',
        'message' => 'Welcome to CodeIgniter'
	);
$this->load->view('about_us', $data);

applications/controllers/Welcome.php

class Welcome extends CI_Controller {

	public function about_us() {
		$data = array(
	        'title' => infovistar.com',
	        'heading' => About Us',
	        'message' => 'Welcome to CodeIgniter‘'
		);

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

}

applications/views/about_us.php

<html>
<head>
    <title><?php echo $title; ?></title>
</head>
<body>
    <h1><?php echo $heading; ?></h1>
	<h6><?php echo $message; ?></h6>
</body>
</html>
Views