D
D
Dmitry2017-05-26 21:48:12
Yii
Dmitry, 2017-05-26 21:48:12

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];
    }
}

It turns out the following.
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
            )
    )
)

Arrays containing id 154, 163, 182 are flights "there", an array with id 98 is a flight "back".
But in this option, I do not pull up connections with other models, for example, I need city_start_id to be not the city id, but the name.
You can, of course, in the same cycle make a substitution
foreach($dataOne as $key => $value){
   $value->city_start_id = $value->cityStart->city;
   /*****/
}

But how right is this?
How to use connections in this case?
Or am I not doing it at all?
How to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-05-27
@slo_nik

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];
    }
}

To display the result in the ListView, I made two views, one for the search results "there" and "back" and one for the search results in one direction
'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]);
                   }
}

Now, when the user searches for tickets "there" and "back" one view is connected, if only "there" - a different view.
In the first case, it looks like this:
In the second, like this:
Maybe it would be more correct to predefine the widget method itself, but...
In general, the result suits me.
I thank everyone who tried to help in solving the problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question