Есть обычная модель (из кода здесь временно убрал кое-что inheritdoc, аттрибутс и тд., чтоб меньше места занимало)
Код: Выделить всё
<?php
namespace app\modules\tour\models;
use Yii;
class TourPlayoff extends \yii\db\ActiveRecord
{
public static function tableName()
{
return '{{%tour_playoff}}';
}
public function rules()
{
return [
[['part', 'tour_id'], 'required'],
[['id', 'part', 'position', 'tour_id', 'team_id'], 'integer'],
];
}
высчитываю высоту для разделителя между ячейками, чем больше стадия тем больше должен быть разделитель между ячейками
public function getHeight($n, $r)
{
$position = $n + 1;
if ($r < 2) $height = 70;
if ($r == 2) $height = 90;
if ($r == 3) $height = 135;
if ($r == 4) $height = 210;
if ($r > 4) $height = 250;
return [
'height' => $height,
'position' => $position,
];
}
public function getTeam()
{
return $this->hasOne(\app\modules\team\models\Team::className(), ['id' => 'team_id']);
}
}
$playoff = new TourPlayoff();
return $this->render('view', [ 'playoff' => $playoff,]);
и вот самая сложность, это вывод
пока пхп код во вьюхе, я понимаю что это надо все в модель перекинуть или в вспомогательный класс, вот с этим и хочу разобраться как это грамотно и правильно сделать
Код: Выделить всё
<?php
use yii\helpers\Html;
use app\modules\tour\models\TourPlayoff;
// узнаю сколько команд всего, пока они все на первой стадии
$countPart1 = TourPlayoff::find()->where(['tour_id' => $model->id, 'part' => 1])->count();
// получаю кол-во стадий плейофф например 1/4 , 1/2 , final это три стадии(round)
$round = 1;
for ($i = $countPart1; $i > 1; $i = $i / 2)
$round++;
$col = $countPart1;
// получаю данные из базы
$new = TourPlayoff::find()->with([ 'team'])->asArray()->where(['tour_id' => $tour_id])->orderBy(['position' => ''])->all();
// тут создаю нужный массив, который потом буду выводить в хтмл
for ($stage = 0; $stage < $round; $stage++) {
for ($iteration = 0; $iteration < $col; $iteration++) {
foreach ($new as $key => $var) {
if ($iteration == $stage) {
if ($var['part'] == $iteration + 1) {
$data[$iteration]['position'][] = $var['position'];
$data[$iteration][$var['position']]['name'] = $var['team']['name'];
}
}
}
}
}
?>
<!-- пошел вывод в хтмл сетки -->
<table>
<tr>
<?php for ($stage = 0; $stage < $round; $stage++): ?>
<td>
<?php for ($stageInside = 0; $stageInside <= $round; $stageInside++) : ?>
<table>
<?php if ($stage == $stageInside): ?>
<?php if ($stageInside != 0) {
$col = $col / 2;
} ?>
<?php for ($iteration = 0; $iteration < $col; $iteration++) : ?>
<?php
$array = $playoff->getHeight($iteration, $stageInside);
$height = $array['height'];
$position = $array['position'];
?>
<tr>
<td class="pf_line">
<?php if ($iteration % 2 == 0 AND $stageInside < 1 AND $iteration != 0): ?>
<div class="pf_separator"></div>
<?php endif; ?>
<?php if ($iteration % 2 == 0 AND $iteration != 0): ?>
<div class="pf_separator"
style="height:<?= $stageInside * $height ?>pt;"></div>
<?php endif; ?>
<?php if (@in_array($position, $data[$stage]['position'])): ?>
<div class="pf_team">
<?= $data[$stage][$position]['name'] ?>
</div>
<?php else: ?>
<div class="pf_team"></div>
<?php endif; ?>
</td>
</tr>
<?php endfor; ?>
<?php endif; ?>
</table>
<?php endfor; ?>
</td>
<?php endfor; ?>
</tr>
</table>
Вот сам массив с данными которые берутся из базы
Код: Выделить всё
[
0 => [
'id' => '1'
'part' => '1'
'position' => '1'
'tour_id' => '4'
'team_id' => '30'
'team' => [
'id' => '30'
'name' => 'CSKA Moscow'
'category_id' => '1'
'country_id' => '1'
'pic' => '58624614d2b52.png'
]
]
1 => [
'id' => '5'
'part' => '2'
'position' => '1'
'tour_id' => '4'
'team_id' => '30'
'team' => [
'id' => '30'
'name' => 'CSKA Moscow'
'category_id' => '1'
'country_id' => '1'
'pic' => '58624614d2b52.png'
]
]
2 => [
'id' => '7'
'part' => '3'
'position' => '1'
'tour_id' => '4'
'team_id' => '32'
'team' => [
'id' => '32'
'name' => 'Spartak Moscow'
'category_id' => '1'
'country_id' => '1'
'pic' => '58624617163e3.png'
]
]
3 => [
'id' => '2'
'part' => '1'
'position' => '2'
'tour_id' => '4'
'team_id' => '31'
'team' => [
'id' => '31'
'name' => 'Lokomotiv Moscow'
'category_id' => '1'
'country_id' => '1'
'pic' => '58624615ea012.png'
]
]
4 => [
'id' => '6'
'part' => '2'
'position' => '2'
'tour_id' => '4'
'team_id' => '32'
'team' => [
'id' => '32'
'name' => 'Spartak Moscow'
'category_id' => '1'
'country_id' => '1'
'pic' => '58624617163e3.png'
]
]
Код: Выделить всё
[
0 => [
'position' => [
0 => '1'
1 => '2'
2 => '3'
3 => '4'
]
1 => [
'name' => 'CSKA Moscow'
]
2 => [
'name' => 'Lokomotiv Moscow'
]
3 => [
'name' => 'Spartak Moscow'
]
4 => [
'name' => 'Zenit St. Petersburg'
]
]
1 => [
'position' => [
0 => '1'
1 => '2'
]
1 => [
'name' => 'CSKA Moscow'
]
2 => [
'name' => 'Spartak Moscow'
]
]
2 => [
'position' => [
0 => '1'
]
1 => [
'name' => 'Spartak Moscow'
]
]
]
Код: Выделить всё
.pf_team {
width: 200px;
height: 40px;
border: 1px solid #c0c0c0;
border-radius: 3px;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
padding:6px;
margin:5px;
}
.pf_separator { height: 20px; }
.pf_line { border-right:1px #F0DBB4 solid; border-top:1px #F1F1E3 solid; border-bottom:1px #F1F1E3 solid }