F
F
FrelFrloich2019-03-16 06:45:58
Yii
FrelFrloich, 2019-03-16 06:45:58

Yii2 php foreach and how to favorably set if conditions?

I understand that it is not clear from the question what I want, I could not formulate.
And so, there is sorting in the controller.

public function actionIndex()
    {
        $server_list_earli = Listserver::find()->orderBy(['status'=> SORT_ASC, 'date' => SORT_ASC])->all();                         
                         
        return $this->render
        ('index',['list_earlier' => $server_list_earli]);
    }

index.php has foreach and conditions in it.
<div class="box__list box__solar">
        <?php $showed = false; ?>
          
        <?php foreach($list_earlier as $list):?>
          <?php 
          if ( $list->date_start < date("Y-m-d") && $list->date_end > date("Y-m-d") ) {
            $status = $list->status;
          }else {
            $status = 'simple__content__section'';
          }

          if ( $status == 'content__section' ) {
            $icon_star = $a;
            $priority = 1;
            $icon_round = $list->icon_item;
          }else if ( $status == 'midle__content__section' ) {
            $icon_star = $b;
            $priority = 2;
            $icon_round = $list->icon_item;
          }else if ( $status == 'under__content__section' ) {
            $icon_star = $c;
            $priority = 3;
            $icon_round = $list->icon_item;
          }else if ( $status == 'simpler__content__section'' ) {
            $icon_star = ' ';
            $priority = 5;
            $icon_round = ' ';         
          }
          ?>  
          <?php if ($list->date >= date('Y-m-d')): ?>
            <?php  if ($priority < 3): ?>
              <?php if(!$showed){echo '<div class="box__headline heading__hight"><h3>Заголовок<span class="title__line"></span>Сервера <i class="fa fa-usd" aria-hidden="true"></i></h3></div>';$showed = true;} ?>
              <div class="box__span <?= $status ?>">
                <span class="b__link__item item__icon"><?= $icon_star ?></span>
                <span class="b__link__item item__name"><?= $list->name ?></span>
                <span class="b__link__item item__bonus"><?= $icon_round?></span>
                <span class="b__link__item item__date"><?= Yii::$app->formatter->asDate($list->date) ?></span>   
              </div>
            <?php endif; ?>
          <?php endif; ?>
        <?php endforeach;?>
       </div>
     </div>

the idea of ​​all this disgrace was this (don't judge strictly, I've been studying recently and with fits and starts lately, I'm an amateur and I don't make money on it). That everything clung to the given $status = $list->status; the status through the base is set with the start date and the end, if the date is over then $status = 'simple__content__section''; and accordingly everything automatically subsides (icons, css class, etc.), but at the same time I can change everything I need through the database ....
Everything works like a clock (although I'm more than sure that the code is not written this way and there is some solution more comfortable). But there is a small but, such enumeration of the array changes only the condition with the date and the variable.
<?php if ($list->date >= date('Y-m-d')): ?>
            <?php  if ($priority < 3): ?>

Unfortunately, I have to repeat multiple conditions in each iteration, and I have 5 of them.
My personal attempts boiled down to the fact that the variable is equal to the value from the database and I could not apply it in no enumeration.
The question is, how is it possible to avoid this repetition, I would be very grateful if you share a link where I can go for such an inexperienced one.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arman, 2019-03-16
@Arik

Try Widgets , so you will have one place to sort through and you can pass different conditions, as an option, look widgets out of the box , it may be more convenient to inherit from someone

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question