0

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

blog/search?query=post/5

Where I am trying to get the value of $start=5 from the URL using:

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

It is not returning anything.

Where removing ?query=post, i.e, blog/search/5 works fine. But I want to keep the query parameter and read the value.

1

1 Answer 1

1

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():

parse_url('blog/search?query=post/5')
[
    "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:

blog/search/5?query=post

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you for sharing the knowledge. but i am using CI pagination library where i am defining $config['base_url'] = base_url().'blog/search?query=' . urlencode($query); . Is there anyway to tell CI pagination library to make the "base_url" like this--> blog/search/5?query=post. Thank you again
See CodeIgniter's reuse_query_string config for its pagination library. Or you can completely convert pagination params to querystring by setting page_query_string config to true. Read the docs.
Thanks a lot it works great now. I did $config['reuse_query_string'] = true; $config['base_url'] = base_url().'blog/search';
The same docs I scrolled in front of my eyes, in the search of result my mind was tired. Well Thank you..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.