Al-Amin's Blog

How to pass some values from one view to another view in CodeIgniter?

  Al-Amin Sarker  |   27 Jan, 2018 |   CodeIgniter |   view 156 times

CodeIgniter is a super cool PHP software framework. You can easily handle it. Variable is a power in a program. CodeIgniter gives you multiples way to set your variables in your program. So, you can easily set a variable in a controller class and use it to your view template. Cool! now I'm going to create a Controller under the controller segment for set variables and show it to view to another view template.


 
	class Category extends CI_Controller {

		public function index()
		{
			/** Normal or regular variable for show view template */
			$data['color'] = 'Red';

			/** For set view to another view template variable */
			$data['new_color']['set_color'] = 'Chocolate';

			/** Load home view template */
			$this->load->view( 'home', $data );
		}
	}


Great! That Category class I was created with two variables and load home view template. Now I want to use those variables for values in a main(home) view template and a nested(about) view template. Okay, now let's start to create home view template in CodeIgniter view directory.

home.php view template.


 
	/** show Red value */
	echo $color;

	/** Load nested view template */
	$this->view( 'about', $new_color );

That home view template will show you Red value with load nested ( about ) view template. Now I'm going to create about template in same view directory.

about.php view template.


 
	/** here use second variable that I created in Controller */
	echo $set_color;

This about view template will show you the Chocolate value in the home view template. Because I called about view template from home view template. Okay ?

Hope, you enjoyed it!

Subscribe to newsletter

About Al-Amin Sarker

Profile Picture

Al-Amin Sarker is a Bangladesh times best Web Engineer. He helps to build web based software. He studied at Computer Science and Engineering in Rajshahi University, Rajshahi, Bangladesh. He has more than 6 years of experience on building robust and scalable web applications (not websites) with cutting edge technologies Continue reading...

Do you love my source code ?

Hire me to Develop

Do you want me to
Develop at
Your Software ?
Click here to Hire

facebook box