Passing Parameters in CodeIgniter.
Note: I have first learned this from PHPEverday.com. The tutorials there works for version 1.5 and doesn't seem to work in 2.1.0. The code below is my modified version that works on CodeIgniter Version 2.1.0
Parameter is a special kind of variable that is used to pass data to a subroutine. In this code, the array $data is used as the parameter to pass the values of $name and $color to the page you_view.php
I modified the hello.php from my previous post Creating My First CodeIgniter App.
Image 1: The modified hello.php (controller)

In this version:
Lines 4 and 5: I declared the variables $name and $color;
Lines 12 and 14: Assigned values to the $name and $color respectively
Lines 15 and 16: The contents of the variables $name and $color were copied to the parameter named $data.
Line 18: Loads the view page named you_view with the array $data passed as parameter.
Image 2: you_view.php
As can be seen, there's no need to extract the individual data ($name and $color) from the array $data that was passed from the controller.
In your browser, once again you enter : localhost/index.php/hello/you. You will get:
Image 3: The Output
In this example, if you want to change the name on the output, you simply change the value of the variable $name on the controller. Same thing if you want to change the color.
Do some experiments if you want to. If you have any questions or comments feel free to post it.


Comments
Post a Comment