简体   繁体   中英

How to group models array in yii2

Hi everyone i have a page on which i am displaying data using foreach loop from models . I wants to know that how can i group those classes and display accordingly.

For example I have registration forms on the page which has dates and classes. So i want to group them by dates first and than classes.My registration models has fields like below.

Registration
id
event_id
dates
class_id
user_id
title
description

Now on this view file i am passing array of $modelRegistration so i dont actually have simple multi dimensional array so i cant use that logic to group that and display.Here is my view code to display the forms

<table class="table table-bordered table-striped">
<thead>
<tr>
    <th>Date</th>
    <th>Class</th>
    <th>User Name</th>
    <th>Title</th>
    <th>Description</th>
</tr>
</thead>
<tbody class="container-items">


<?php foreach ($modelsRegistration as $indexRegistration => $modelRegistration): ?>
    <tr value="<?= $modelRegistration->date ?>" class="house-item">
        <td class="col-lg-1 col-md-1 col-sm-1 col-xs-1"><?= $modelRegistration->class->name ?></td>
        <td class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
            <label><?= $modelRegistration->user->name ?></label>
        <td class="col-lg-2 col-md-2 col-sm-2 col-xs-2"><?= $modelRegistration->title ?> </td>
        <td class="col-lg-1 col-md-1 col-sm-1 col-xs-1"><?= $modelClass->description ?> </td>
    </tr>
<?php endforeach; ?>
</tbody>

I want to get array like below

Registration
(
[0] => 10/12/2015
(
    [0] => class-1(
              [0] => array(
                      [title] => "Title 1"
                      [id] => 5
                      [username] => John 
                      [Description] => Team Leader
                     )
              [1] => array(
                      [title] => "Title 2"
                      [id] => 6
                      [username] => John 1 
                      [Description] => Team 2
                     )
     )
    [1] => class-2(
              [0] => array(
                      [title] => "Title 4"
                      [id] => 7
                      [username] => John 4 
                      [Description] => Team 4
                     )
              [1] => array(
                      [title] => "Title 5"
                      [id] => 9
                      [username] => John 5 
                      [Description] => Team 6
                     )
     )
    [2] => class-3(
              [0] => array(
                      [title] => "Title 1"
                      [id] => 5
                      [username] => John 
                      [Description] => Team Leader
                     )
              [1] => array(
                      [title] => "Title 2"
                      [id] => 6
                      [username] => John 1 
                      [Description] => Team 2
                     )
     )
)
[0] => 12/12/2015
(
     [0] => class-1(
              [0] => array(
                      [title] => "Title 8"
                      [id] => 12
                      [username] => John 10
                      [Description] => Team 77
                     )
              [1] => array(
                      [title] => "Title 27"
                      [id] => 67
                      [username] => John 17 
                      [Description] => Team 27
                     )
     )
    [1] => class-3(
               [0] => array(
                      [title] => "Title 41"
                      [id] => 71
                      [username] => John 41 
                      [Description] => Team 41
                     )
              [1] => array(
                      [title] => "Title 51"
                      [id] => 59
                      [username] => John 54 
                      [Description] => Team 64
                     )
     )
)

If i can group it by dates first and than classes than i want to display the date only once and than under that date all the grouped classes and under classes i can display other information.Thank you

Map Your Model Data using date group Array helper Map Group in yii2

$model = Registration::findAll();
$registrationModels = ArrayHelper::map($model , 'event_id' ,'class_id' ,'dates');

Suppose that your model name is Registration and view name is vrego.

  1. In your controller

    // select all and order by date and class_name

     $registrations= Registration::find() ->orderBy('date, class_name') ->all(); return $this->render('vrego', [ 'registrations' => $registrations, ]); 
  2. Then in your view loop through $registrations and display each record.

i am geting the result i tried my self : you need to create a class of Registration and i think you created that one then use this below query you will get that info .

 $events  = Registration::find()->all();
        foreach($events as $event){
            $new_array[$event->dates_date][][$event->class_id][]=$event;
        }
<pre>
after print_r($new_array);
<pre>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM