Welcome Guest, Not a member yet? Register   Sign In
Need help with $uri->addQuery
#1

Hi,

I have a view that shows a list of bookings. By default that list shows the bookings to come in a future date. I want to offer the possibility to filter that list so a user can see all the bookings (including the ones in the past) and can filter on those who are still open to registration. I am using a get route to pass the search query. Things are working well, my controller gets the search query and I get the results that are filtered properly. 

I am now at the stage where I want to add the filters on the view. I am using links to add a parameter / or to remove one. I thought I'd use the $uri->addQuery to create the links. And I created a little helper that should return the modified url to each links. But It looks like the returns 'are cumulative' and I am not making sense of what's happening.

Here's my helper:
PHP Code:
function addParamQuery($param,$value) {
    $uri current_url(true);
    $uri->addQuery($param$value);
    return $uri;

    
And in my view, my filter links using the helper:
PHP Code:
    <a href="<?= addParamQuery('registration', 'open') ?>">show open bookings</a>
    <a href="<?= addParamQuery('date', 'all') ?>">show also past dates</a

But here's the html I am getting when I am viewing /agenda :
Code:
<a href="http://localhost:8080/en/agenda?registration=open">show open bookings</a>
<a href="http://localhost:8080/en/agenda?registration=open&amp;date=all">show also past dates</a>

I was expecting to get for the second link only agenda?date=all . Why is registration=open part of the url returned?

It looks like each call 'builds' on the previous rather than starting from the current url (without any parameter). I am not understanding why... and what I need to do to fix it.  Huh

Can anyone help?
Reply
#2

Everything is right. You are using the same object. Learn PHP syntax)
Try as 
PHP Code:
$uri = clone current_url(true); 
Simple CI 4 project for beginners codeigniter-expenses
Reply
#3

Somehow I thought that calling current_url would "start from scratch" if I can express myself like that. I did not think there would be a persistent object from one call to the other one.
I did not know about the clone function.
Thanks for your quick help, I am learning a lot indeed.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB