Passing data for views from controllers in Codeigniter

Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading method.

Controller File (Hello.php) [<yourproject>/application/controllers/Hello.php]

<?php class Hello extends CI_Controller { public function index() { $rawArray = array( 'oranges' => 'fruits',
				'computer' => 'electronics',
				'wheat' => 'grains'
		);

		$this->load->view("first", $rawArray);
	}
}

View File (first.php) [/application/views/first.php]

<html>
<head>
<title>My first view</title>
</head>
<body>


		<?php

		echo $oranges . "
";
		echo $computer . "
";

		?>
	


</body>
</html>

Output

Untitled

Share

You may also like...