H
H
hollanditkzn2018-03-22 15:20:08
PHP
hollanditkzn, 2018-03-22 15:20:08

How in a function to return foreach the entire selection?

I use a function to shorten the code and avoid repeating code in the view, but I can't implement foreach inside the function. If I return the foreach value, then there will be only one selection, and it does not process everything else. If echo is done instead of return, then the record comes across in the wrong place. That is, it needs to be written inside the td cell A is displayed behind this tag

function shift($shop_id, $day){
    $monday = Shifts::find()->andWhere(['shop_id' => $shop_id])->andWhere(['>=', 'start_date', date('Y-m-d 00:00:00', strtotime($day))])->andWhere(['<=', 'start_date', date('Y-m-d 23:59:59', strtotime($day))])->all();
    foreach ($monday as $employee){
        return Yii::$app->formatter->asTime($employee->start_date, 'php:H:i').'-'.Yii::$app->formatter->asTime($employee->end_date, 'php:H:i').' '.$employee->user->nameEmployee.'<br>';
    }
}
<table class="table">
        <tr>
            <th>Магазины</th>
            <th>Пн <?= date("d.m", strtotime("last Monday")) ?></th>
            <th>Вт <?= date("d.m", strtotime("last Tuesday")); ?></th>
            <th>Ср <?= date("d.m", strtotime("last Wednesday")); ?></th>
            <th>Чт <?= date("d.m", strtotime("Thursday")); ?></th>
            <th>Пт <?= date("d.m", strtotime("Friday")); ?></th>
            <th>Сб <?= date("d.m", strtotime("Saturday")); ?></th>
            <th>Вс <?= date("d.m", strtotime("Sunday")); ?></th>
        </tr>
        <?php foreach ($shops as $shop){
            echo '<tr>
                <td>'.$shop->name.'</td>';
            echo '<td>'.shift($shop->id, 'last Monday').'</td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>';
        } ?>
    </table>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ilyale, 2018-03-23
@hollanditkzn

The approach itself, where layout and logic are mixed, let's say, has the right to exist in limited specific tasks ...
Regarding the solution, as far as I could understand the flow of the author's thoughts, one cycle through the $shops records is not enough.
Your first loop iterates over the rows in the $shops array and renders a row in the layout to display the data row. This is within the line boundaries.
Between them (in place of the ellipsis) there should be a cycle by the days of the week, where your function is called, which processes the data of one day.
Hope it helps.
PS IMHO. In general, the approach when you retrieve an array of data (you have an array of $shops), and then go through it in a loop and each iteration of the loop climbs into the database - this is low-performance and redundantly loads the database (in other words, this is shit code).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question