За последние 24 часа нас посетили 62917 программистов и 1679 роботов. Сейчас ищут 982 программиста ...

Как сделать таблицу в foreach

Тема в разделе "PHP для новичков", создана пользователем myks92, 12 июн 2018.

  1. myks92

    myks92 Новичок

    С нами с:
    12 июн 2018
    Сообщения:
    45
    Симпатии:
    1
    PHP:
    1. <div class="print">
    2.                     <?php foreach ($models as $model):?>
    3.                         <?php if ($clubId !== $model->profile->club_id):?>
    4.                             <?php if ($model->profile->club_id):?><span>Коллектив: <strong><?=$formatter->asText($clubName = $model->profile->club->name)?></strong></span><?endif;?>
    5.                             <?php if ($model->profile->city_id):?><span>Город: <strong><?=$formatter->asText($model->profile->city->name)?></strong></span><?endif;?>
    6.                             <?php if ($model->profile->club_id):?><span>Руководитель: <strong><?=$formatter->asText($model->profile->club->chiefId1->getFullName())?></strong></span><?endif;?>
    7.                         <? endif; ?>
    8.                             <?php if ($userId !== $model->user_id):?>
    9.                                 <h4><?=$model->profile->getFullName()?></h4>
    10.                             <? endif; ?>
    11.                                 <table class="table table-bordered table-payment">
    12.                                     <thead>
    13.                                         <tr class="table-head">
    14.                                             <td>№ участника</td>
    15.                                             <td>Ранг</td>
    16.                                             <td>Дисциплина</td>
    17.                                             <td>Номинация</td>
    18.                                             <td>Возрастная категория</td>
    19.                                             <td>Композиция</td>
    20.                                             <td>Взнос</td>
    21.                                         </tr>
    22.                                     </thead>
    23.                                     <tbody>
    24.                                         <tr class="table-body">
    25.                                             <td><?=$formatter->asInteger($model->number)?></td>
    26.                                             <td><?=$formatter->asText($model->rang->getShortName())?></td>
    27.                                             <td><?=$formatter->asText($model->discipline->getShortName())?></td>
    28.                                             <td><?=$formatter->asText($model->nomination->getShortName())?></td>
    29.                                             <td><?=$formatter->asText($model->category->getShortName())?></td>
    30.                                             <td><?=$formatter->asText($model->music?$model->music->composition_name:null)?></td>
    31.                                             <?php if ($model->price):?><td><?=$formatter->asText($model->price->cost)?></td><?else:?><td>—</td><?endif;?>
    32.                                         </tr>
    33.                                     </tbody>
    34.                                 </table>
    35.                         <?php
    36.                             $clubId = $model->profile->club_id;
    37.                             $userId = $model->user_id;
    38.                         ?>
    39.                     <?php endforeach;?>
    40.                 </div>
    Мне нужно объединить по пользователю <tbody>
     

    Вложения:

  2. igordata

    igordata Суперстар
    Команда форума Модератор

    С нами с:
    18 мар 2010
    Сообщения:
    32.408
    Симпатии:
    1.768
    нипанятна
     
  3. username

    username Новичок

    С нами с:
    6 июл 2017
    Сообщения:
    223
    Симпатии:
    17
    PHP:
    1. <div class="print">
    2. <?php
    3.   $m = array();
    4.  
    5. foreach ($models as $model) {
    6.         $shapka = '';
    7. if ($clubId !== $model->profile->club_id) {
    8.   if ($model->profile->club_id) $shapka.= '<span>Коллектив: <strong>'.
    9.       $formatter->asText($clubName = $model->profile->club->name).'</strong></span>';
    10.   if ($model->profile->city_id) $shapka.= '<span>Город: <strong>'.
    11.     $formatter->asText($model->profile->city->name).'</strong></span>';
    12.   if ($model->profile->club_id) $shapka.= '<span>Руководитель: <strong>'.
    13.       $formatter->asText($model->profile->club->chiefId1->getFullName()).'</strong></span>';
    14. };
    15.  
    16. if ($userId !== $model->user_id) $shapka.= '<h4>'.$model->profile->getFullName().'</h4>';
    17.  
    18. $stroka = '                              <tr class="table-body">
    19.                                            <td>'.$formatter->asInteger($model->number).'</td>
    20.                                            <td>'.$formatter->asText($model->rang->getShortName()).'</td>
    21.                                            <td>'.$formatter->asText($model->discipline->getShortName()).'</td>
    22.                                            <td>'.$formatter->asText($model->nomination->getShortName()).'</td>
    23.                                            <td>'.$formatter->asText($model->category->getShortName()).'</td>
    24.                                            <td>'.$formatter->asText($model->music?$model->music->composition_name:null).'</td>
    25. ';
    26. if ($model->price) $stroka.='<td>'.$formatter->asText($model->price->cost).'</td>';
    27. else $stroka.='<td>—</td>';
    28.                             $clubId = $model->profile->club_id;
    29.                             $userId = $model->user_id;
    30. if (!isset($m[$userId])) $m[$userId]=array($shapka);
    31.   $m[$userId][] = $stroka;
    32. }
    33.  
    34. foreach ($m as $userId=>$stroki) {
    35.   echo array_shift($stroki);
    36.   echo '
    37.                                <table class="table table-bordered table-payment">
    38.                                    <thead>
    39.                                        <tr class="table-head">
    40.                                            <td>№ участника</td>
    41.                                            <td>Ранг</td>
    42.                                            <td>Дисциплина</td>
    43.                                            <td>Номинация</td>
    44.                                            <td>Возрастная категория</td>
    45.                                            <td>Композиция</td>
    46.                                            <td>Взнос</td>
    47.                                        </tr>
    48.                                    </thead>
    49.                                    <tbody>';
    50.   foreach ($stroki as $e) echo $e;
    51.   echo '                                  </tbody>
    52.                                </table>';
    53. };?>
    54.                 </div>