【问题标题】:CodeIgniter - Get Last URI SegmentCodeIgniter - 获取最后一个 URI 段
【发布时间】:2010-09-30 10:41:34
【问题描述】:

我正在尝试获取 CI 中的最后一个 URI 段,但是我不知道它的编号是多少,因为当用户单击页面中的链接时,将附加参数(整数)。然后在控制器中使用它们通过 ajax 将相关的数据库记录拉入页面。

如何告诉 CI 获取最后一段?

类似:

$record_num = $this->uri->segment($last);

【问题讨论】:

  • 感谢您的回复,两个答案都很好。我去了 Anpher's,因为它更简洁一些。

标签: php codeigniter


【解决方案1】:
$record_num = end($this->uri->segment_array());

【讨论】:

  • 嗨!我使用了这段代码,但是当找不到该段时,它会抛出错误,请帮忙。我在 if 条件中写了空但不工作。
  • 总是像public function name($parameter = " ")一样声明你的参数
  • Only variables should be passed by reference。请改用 Mischa 答案。
【解决方案2】:

这应该可行:

$last = $this->uri->total_segments();
$record_num = $this->uri->segment($last);

【讨论】:

  • +1 我更喜欢这种方法,因为它使用了 codeigniter 的功能。这似乎是正确的做法。
  • 我同意,更喜欢这个。您也可以为自己节省一行: $record_num = $this->uri->segment($this->uri->total_segments());
【解决方案3】:

我在 2021 年需要这个,这就是我如何做到的

$record_num = end($this->uri->segments)

Ty to Anpher 让我走上了正轨

【讨论】:

    猜你喜欢
    • 2012-03-02
    • 2011-09-09
    • 2011-05-02
    • 1970-01-01
    • 2015-03-07
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多