Jaydeep Goswami Jaydeep Goswami - 1 year ago 78
PHP Question

URI segment does not get returned when using querystrings

I'm trying to paginate the search results returned by the query. I have the following URL:


Where I am trying to get the value of
from the URL using:

$start = $this->uri->segment(3);

It is not returning anything.

Where removing
, i.e,
works fine. But I want to keep the query parameter and read the value.

Answer Source

That's because of the ? character in the URI. CodeIgniter URI parser (and any other standard URI parser) does not recognize what you have in mind. After the ? character, it's all query string, not URI segments. See the output of PHP's parse_url():

    "path" => "blog/search",
    "query" => "query=post/5",

See? The first segment is blog, the second is search and the rest is the querystring.

You need to change your URI design in an standard way. For example:


So that the call to $this->uri->segment(3) will return what you have in mind.

Speaking of CodeIgniter pagination library, see reuse_query_string and page_query_string configs in the documentation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download