16

sorry to bother but I was hoping someone could help me out with a quite mundane issue that I am having within CI. I am able to send a variable via the URL using CI's examples eg:

http://localhost/project/main/getproduct/24

within the getproduct() method of my main controller I can get the variable sent 24, without an issue.

however I now want to pass two variables via the URL, but I have no idea how to do this or whether CodeIgniter will allow me to do this. could someone please show me how to pass 2 variables in CI and a method that can retrieve them I have tried:

http://localhost/project/main/getproduct/24/45

and then within my getproduct method:

public function getproduct($productID, $factoryID){
  .....
}

but I'm finding my method can get the first variable without an issue but not the second variable. Could anyone point me in the right direction please. Many thanks in advance.

1
  • What does your getproduct method look like? Commented Mar 31, 2014 at 15:45

8 Answers 8

33

The accepted answer will work for this particular issue, but will not work if the url ever changes. To access multiple variables in your controller, simply add to the function definition.

http://localhost/project/main/getproduct/24/45

class Main extends CI_Controller {

    public function getproduct($productID = 0, $factoryID = 0)
    {
      // ProductID will be 25
      // Factory ID will be 45
    }
}

Reference: CodeIgniter User Guide

Sign up to request clarification or add additional context in comments.

1 Comment

this worked but without routes. when trying to add routes it is not working
28

You can use uri to retrieve values in your url

Here is an example

public function getproduct()
{
  $productID =  $this->uri->segment(3);
  $factoryID =  $this->uri->segment(4);
  // ProductID will be 25
  // Factory ID will be 45
}

Then you can just use the values as you please

2 Comments

I do not agree that this is the way to do it. You should not be reading the variables like that. CodeIgniter has a specific way to get that data without the need for two extra lines of code. See answer below stackoverflow.com/a/22774721/1084306
hey, do what Donovan says.
12

You must set a route in config/routes.php to parse the items.

It looks like:

   $route["getproduct/(:any)/(:num)"]="main/changequestion/$1/$2"

Then i hope it will work.

5 Comments

how would I then retrieve the variables in the method?
I have never had to to that - it's always just worked for me.
Try this: ellislab.com/codeigniter/user-guide/general/controllers.html . At Passing URI Segments to your Functions . Hope it helps . I think if you use more than 1 paramter when uri routing is active you can get them with uri->segment only. Getting them with uri segment It is basically the same thing.
I see... I've never used the URI routing feature. Thanks!
You do not HAVE to put a route in place to get these variables. It is already built into the controller to accept variables in the function.
4

If someone else runs into this using CI3. In CodeIgniter 3 no special route is needed. Not sure if it also works on CI2 now.

You can access those URI segments using parameters just like that:

http://localhost/project/main/getproduct/24/45

public function getproduct($productID, $factoryID){
  .....
}

Comments

2

You can use uri to retrieve values in your url http://localhost/project/main/get_product/12/23

Here is an example

public function get_product(){
  $product_id =  $this->uri->segment(3);  // Product id will be 12
  $factory_id =  $this->uri->segment(4);  // Factory id will be 23

}

Then you can just use the values as you please

Comments

0

http://example.com/project/main/getproduct/24/45

To get '45', you can do this:

 $id1 =  $this->uri->segment(3);
 echo $id1; //output is 45

Comments

0

Passing URI Segments to your methods

If your URI contains more than two segments they will be passed to your method as parameters.

For example, let’s say you have a URI like this:

example.com/index.php/products/shoes/sandals/123

Your method will be passed URI segments 3 and 4 (“sandals” and “123”):

<?php
class Products extends CI_Controller {

        public function shoes($sandals, $id)
        {
                echo $sandals;
                echo $id;
        }
}

Important!!! If you are using the URI Routing feature, the segments passed to your method will be the re-routed ones.

Refer to this link as Codeigniter Official Guide. Codeigniter Official Guide.

Comments

0

Solution of this problem is using of _remap() function. You just need to add this function before index() function

function _remap($method, $args)
{

       if (method_exists($this, $method))
       {
           $this->$method($args);
       }
       else
       {
            $this->Index($method, $args);
       }
}

I hope this will solve your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.