Code igniter parameter passing techniques
Upcoming SlideShare
Loading in...5
×
 

Code igniter parameter passing techniques

on

  • 12,029 views

Code igniter parameter passing techniques

Code igniter parameter passing techniques

Statistics

Views

Total Views
12,029
Slideshare-icon Views on SlideShare
12,026
Embed Views
3

Actions

Likes
0
Downloads
120
Comments
0

2 Embeds 3

http://www.linkedin.com 2
http://www.slideshare.net 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Code igniter parameter passing techniques Code igniter parameter passing techniques Document Transcript

    • www.InnovativePhp.comParameter Passing Techniques in CodeIgniter2010-10-10 03:10:08 nimeshrmrI have been using code Igniter framework in my recent projects. Recently one of my friends ran into aproblem when passing optional parameters to controller function. So i decided to write this article to sharethe knowledge i gained by solving that problem.Lets start by using a simple example.1. Accessing paramets as pre defined varibles in controller functionEx : http://www.yoursite.com/index.php/Student/Create/Mark/22class Student extends Controller{public function __construct() { parent::Controller(); } public function create($name,$age){ $studentName = $name; $studentAge = $age; }}When you call a controller function in code igniter the first two parameters after index.php is ControllerClass name and Controller class function respectively. In this method there are two other parametersseparated by ‘/’ . These two components are the two get parameters passes to the functiion. We canaccess these get parameters by using two variables as shown in the example.Name in the create function would be the first parameter in the url after create. So value of $name will beMark.Age in the create function would be the second parameter in the url after create . So value of $age will be22.2. Accessing parameters without defining variables in controller functionEx : http://www.yoursite.com/index.php/Student/Create/Mark/22class Student extends Controller{ public function __construct() { parent::Controller(); } public function create(){ $studentName = $this->uri->segment(3); $studentAge = $this->uri->segment(4); }}This method can be used to get parameters passed to a controller function without defining variablesstatically in the function declaration. In code igniter url parameters can be accessed by using the uri->segment function. $this->uri->segment(param no); // You can use $this when using inside the controller function. Parameter number is required.The first two segments of the url will always be Controller Class and Controller Function inCodeIgniter. So if you want to access parameters , you have to start from index 3. In the above examplewe can access the two parameters using index 3 and 4. This method is useful when the number ofparameters are dynamic.3. Accessing Parameters Using Key Value PairsEx : http://www.yoursite.com/index.php/Student/Create/Name/Mark/Age/22<?phpclass Student extends Controller{ public function __construct() { parent::Controller(); } public function create(){ $parameters = $this->uri->uri_to_assoc(); $studentName = $parameters[Name]; $studentAge = $parameters[Age]; }}?>Consider a situation where the number of parameters passed to a function are dynamic and some of theparameters are optional. Example is given below.http://www.yoursite.com/index.php/Student/Create/Markhttp://www.yoursite.com/index.php/Student/Create/Mark/22In previos two urls the age is optionnal. so we dont pass it every time we use the function . Hence it isdifficult to access the optional parameter through predefined varibales or url indexes.In these type of situations you cannot use the first method and also the second method is very hard touse. We can access parameters with key-value pairs to solve this problem. CodeIgniter provides amethod called uri->uri_to_assoc();So in the url provided in the previos example (Name/Mark/Age/22) will be broken into 2keys and 2 values.Name and Age will be the two keys and Mark and 22 will be the values for those keys respectiively. I preferthis methos since ii know exactly what are the parameters i am accessing.Hope you enjoyed the post on Parameter Passing Techniques in CodeIgniter. Please feel free tocomment with your sugesstions and content of this post.