简体   繁体   English

如何获取CI中的最后两个uri分段?

[英]How to get last two uri segments in CI?

I want to last two uri segment. 我想持续两个uri段。 just for example i have one url like: 例如,我有一个网址,例如:

www.abcd.com/country/state/city AND Next time this url is dynamic so like this: www.abcd.com/country/state/city并且下次该网址是动态的,如下所示:

www.abcd.com/grade/country/state/city

i want to last two uri segments, please help and solve this problem. 我想要最后两个uri段,请帮助解决此问题。

Yaah, i have found solution of this problem 是的,我找到了解决此问题的方法

$record_num = $this->uri->segment_array();
echo "last - ".$record_num[count($record_num)];
echo "<br>second last - ".$record_num[count($record_num)-1];
exit;

You can use uri segments like so. 您可以像这样使用uri细分。 Any thing after www.abcd.com/ is a uri segment www.abcd.com/之后的任何www.abcd.com/都是uri细分

echo $this->uri->segment(1); // This should echo grade

echo $this->uri->segment(2); // This should echo country

echo $this->uri->segment(3); // This should echo state

echo $this->uri->segment(4); // This should echo city

Segment userguide userguide

You can achieve it by using 您可以通过使用

if ($this->uri->segment(1) == "country") {
    echo $this->uri->segment(2); //  echo state

    echo $this->uri->segment(3); //   echo city
}
if ($this->uri->segment(1) == "grade") {
    echo $this->uri->segment(3); //  echo state

    echo $this->uri->segment(4); //   echo city
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM