【问题标题】:How to retrieve value from URL如何从 URL 中检索值
【发布时间】:2019-07-23 02:43:49
【问题描述】:

我想做的是我想从 URL 中获取数据,但是我遇到了一些问题,它取了错误的值

这是我的一个例子

 localhost/index.php/registerform/3/4/5

我尝试了什么:echo $id[3];
代码的结果是:“a”

我想要什么:

从 URL 获取字符串,我想检索“3”并将其设置为 $id

the code $id[3] won't work

我该怎么做?


还有一个问题是可以设置

 localhost/index.php/registerform/3/4/5

进入数组?
localhost 为 [0],index.php 为 [1] 等等。

【问题讨论】:

标签: php codeigniter


【解决方案1】:

从函数中,您可以像这样从 URL 获取变量

 public function function_name($v1, $v2, $v3){
    echo 'Variable one '.$v1.' Variable two '.$v2.' Variable three '.$v3;
 }

public function function_name($v1, $v2, $v3){
    echo 'Variable one '.$this->uri->segment(3).' Variable two '.$this->uri->segment(4).' Variable three '.$this->uri->segment(5);
 }

【讨论】:

    【解决方案2】:

    尝试使用 URI 库中的segment_array()

    在您的控制器中使用以下代码

    echo var_dump($this->uri->segment_array());die;
    

    它将为您提供控制器/方法/3/4/5 作为数组,您可以从数组中分配变量中的每个数组元素

    Array
    (
        [1] => Contoller Name
        [2] => Method Name
        [3] => 3
        [4] => 4
        [5] => 5
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 2017-10-23
      • 2014-10-28
      • 2012-09-17
      • 2013-08-21
      • 1970-01-01
      相关资源
      最近更新 更多