0

I have a simple foreach loop that loops through the current week dates in Y-m-d format:

@foreach($dates as $date)
   <a href="{{route('consols.index', array_merge(\Request::query(), ['date' => date('Y-m-d', strtotime($date))]))}}">{{date('l', strtotime($date))}}</a>
@endforeach

With above, I can simply click on the anchor link, and the specific date will be appended:

example.org/consols?date=2020-03-04

Now I use this to filter a specific SQL query and I wish to be able to filter for multiple dates. For example:

example.org/consols?date=2020-03-04&date=2020-03-05&date=2020-03-06

Where in above example, I wish to send 3 dates as query parameters.

If I simply append another &date= to the already present ?date, it will only select the last date= in the query string.

How can I, with PHP, send multiple variables in my URL with the same parameter name?

1

3 Answers 3

0

Add an integer $i

['date'.$i => date('Y-m-d'

You will have date1= &date2= & date3=

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

Comments

0

You can add your date as array i.e. date[].

Your URL would look like:

example.org/consols?date[]=2020-03-04&date[]=2020-03-05&date[]=2020-03-06

When you console your query it will give you

{ date: [ '2020-03-04', '2020-03-05', '2020-03-06' ] }

Comments

0

I ended up looking at the link CodyKL linked to, and from there saw that I could actually use http_build_query.

This is my solution:


$url = [];
$dates = isset($_GET['date']) ? $_GET['date'] : null;
if($dates){

  foreach($dates as $date){
     $url['date'][] = date('Y-m-d', strtotime($date));
  }

}

$query = http_build_query($url);

Then I can simply just redirect to $query.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.