Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用codeigniter在URL中传递多个变量_Php_Codeigniter_Url_Get_Uri - Fatal编程技术网

Php 使用codeigniter在URL中传递多个变量

Php 使用codeigniter在URL中传递多个变量,php,codeigniter,url,get,uri,Php,Codeigniter,Url,Get,Uri,很抱歉打扰您,但我希望有人能帮我解决我在CI中遇到的一个相当普通的问题。我能够使用CI的示例通过URL发送变量,例如: http://localhost/project/main/getproduct/24 在我的主控制器的getproduct()方法中,我可以24小时发送变量,而不会出现问题 然而,我现在想通过URL传递两个变量,但我不知道如何做,也不知道CodeIgniter是否允许我这样做。有人能告诉我如何在CI中传递2个变量,以及我尝试过的检索它们的方法吗: http://localh

很抱歉打扰您,但我希望有人能帮我解决我在CI中遇到的一个相当普通的问题。我能够使用CI的示例通过URL发送变量,例如:

http://localhost/project/main/getproduct/24
在我的主控制器的getproduct()方法中,我可以24小时发送变量,而不会出现问题

然而,我现在想通过URL传递两个变量,但我不知道如何做,也不知道CodeIgniter是否允许我这样做。有人能告诉我如何在CI中传递2个变量,以及我尝试过的检索它们的方法吗:

http://localhost/project/main/getproduct/24/45
然后在我的getproduct方法中:

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

但是我发现我的方法可以得到第一个变量,而不是第二个变量。谁能给我指一下正确的方向吗。非常感谢。

您必须在config/routes.php中设置路由来解析项目

它看起来像:

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

那么我希望它能起作用。

您可以使用
uri
检索url中的值

这里有一个例子

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

}

然后,您可以随意使用这些值

接受的答案适用于此特定问题,但如果url发生更改,则不起作用。要访问控制器中的多个变量,只需添加到函数定义中

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
    }
}

参考:

如果其他人使用CI3遇到此问题。在CodeIgniter 3中,不需要特殊路线。不确定它现在是否也适用于CI2

您可以使用如下参数访问这些URI段:

要获得“45”,您可以执行以下操作:

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

将URI段传递给方法

如果URI包含两个以上的段,它们将作为参数传递给方法

例如,假设您有这样一个URI:

example.com/index.php/products/shoes/sandals/123
您的方法将被传递到URI段3和4(“檀香”和“123”):


此问题的解决方案是使用_remap()函数。只需在index()函数之前添加此函数


我希望这能解决你的问题

您可以使用
uri
检索url中的值

这里有一个例子

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

}

然后您可以随意使用这些值

您的
getproduct
方法是什么样子的?然后我将如何检索该方法中的变量?我从来没有这样做过-它总是对我有效。尝试以下方法:。在将URI段传递给函数时。希望能有帮助。我认为,如果您在uri路由处于活动状态时使用多个参数,则可以仅使用uri->segment来获取这些参数。用uri段获取它们基本上是一样的。我明白了。。。我从未使用过URI路由功能。谢谢您不必设置路由来获取这些变量。它已经内置在控制器中,用于接受函数中的变量。我不同意这样做。您不应该这样读取变量。CodeIgniter有一种特殊的方法来获取数据,而不需要额外的两行代码。看下面的答案嘿,照多诺万说的去做。这很有效,但没有路线。尝试添加路由时,它不起作用
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

}