Friday, June 24, 2016

Codeigniter tutorial 4

   No comments     
categories: 
Data passing from controller to view

I posted CodeIgniter URL Routing in the controller, Special methods, and Controller and View files earlier.

Now we see, how to pass some data to view files from controller

My view file first_view.php
<html>
<head>
<title>View</title>
</head>
<body>
<h1>This is a view file</h1>
<h2>The UI part</h2>
</body>
</html>

edit this file, give a variable in PHP.

<html>
<head>
<title>View</title>
</head>
<body>
<h1>This is a view file</h1>
<h2>Hello, <?php echo $name ?></h2>
</body>
</html>

in the code\application\controllers edit the hello.php.

<?php
class Hello extends CI_Controller{
public function index(){
$this->first();
}
public function first(){
$data=array("name"=>"Nimishan");
$this->load->view('first_view',$data);
}

}
the input string is assigned into the variable data and the passed to view file.
then the output would be (for URL see image)



Also, the a value can be passed using a parameter.

<?php
class Hello extends CI_Controller{
public function index(){
$this->first();
}
public function first($value){
$data=array("name"=>$value);
$this->load->view('first_view',$data);
}

}

here I add a variable inside the function parenthesis, and it should be assigned to the variable data which is the loaded into the view file. 

0 comments:

Post a Comment