Answer the question
In order to leave comments, you need to log in
Displaying search results in a ListView?
Good evening.
Search for round-trip tickets.
It is required to display search results through ListView.
But you need to group the result in pairs.
That is, there are three flights on the date of departure, and two on the date of return. It should be output in such a way that each departure flight is paired with a return flight.
Now I did it through the foreach () loop, I form two arrays, by the date of departure and the date of return. Then I combine these two arrays into a third array.
$dataOne = $dataProvider->getModels();
foreach($dataOne as $key => $value){
if($value->date_start == strtotime($data['FlightsSearch']['date_from'] . ' 00:00')){
$arr_from[] = $value->attributes;
}
if($value->date_end == strtotime($data['FlightsSearch']['date_to'] . ' 00:00')){
$arr_to[] = $value->attributes;
}
}
foreach($arr_from as $key_f => $from){
foreach($arr_to as $key_t => $to){
$arr_all[] = [$from, $to];
}
}
Array
(
[0] => Array
(
[0] => Array
(
[id] => 154
[city_start_id] => 8
)
[1] => Array
(
[id] => 98
[city_start_id] => 1
)
)
[1] => Array
(
[0] => Array
(
[id] => 163
[city_start_id] => 8
)
[1] => Array
(
[id] => 98
[city_start_id] => 1
)
)
[2] => Array
(
[0] => Array
(
[id] => 182
[city_start_id] => 8
)
[1] => Array
(
[id] => 98
[city_start_id] => 1
)
)
)
foreach($dataOne as $key => $value){
$value->city_start_id = $value->cityStart->city;
/*****/
}
Answer the question
In order to leave comments, you need to log in
Issue resolved. I don't know how true it is, but I got the result I wanted.
I changed my foreach () cycle a little, now I collect in pairs in an array not the attributes of the models, but the models themselves, while saving all the connections that I need.
foreach($dataOne as $key => $value){
if($value->date_start == strtotime($data['FlightsSearch']['date_from'] . ' 00:00')){
$arr_from[] = $value;
}
if($value->date_end == strtotime($data['FlightsSearch']['date_to'] . ' 00:00')){
$arr_to[] = $value;
}
}
foreach($arr_from as $from){
foreach($arr_to as $to){
$arr_all[] = [$from, $to];
}
}
'itemView' => function($model, $key, $index, $widget) use ($search, $data, $arr_all){
if($data['FlightsSearch']['direction'] == 2) {
return $this->render('_item', ['model' => $model, 'search' => $search]);
}
if($data['FlightsSearch']['direction'] == 1) {
return $this->render('_item_to', ['model' => $arr_all, 'search' => $search]);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question