Answer the question
In order to leave comments, you need to log in
How to get rid of foreach error?
I have such a problem in the main.php template, I have a problem and it displays the following entry Invalid argument supplied for foreach(). It's just not clear why foreach is disabled. Most likely, as I understand it, I use it in main. But how to implement the notification system then the question is. I've never done this myself and can't figure out how to do it.
<div class="wrap">
<?php
NavBar::begin([
'brandLabel' => 'Holland',
'brandUrl' => ['/zakaz/index'],
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
if (!Yii::$app->user->isGuest) {
$menuItems[] = ['encode' => false, 'label' => '<span class="glyphicon glyphicon-bell"></span>', 'options' => ['id' => 'notification']];
}
if (Yii::$app->user->isGuest) {
// $menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']];
$menuItems[] = ['label' => 'Войти', 'url' => ['/site/login']];
} else {
$menuItems[] = '<li>'
. Html::beginForm(['/site/logout'], 'post')
. Html::submitButton(
'Выйти (' . Yii::$app->user->identity->username . ')',
['class' => 'btn btn-link logout']
)
. Html::endForm()
. '</li>';
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
?>
<div class="notification-container">
<?php foreach ($notification as $notifications): ?>
<?php echo $notifications->name; ?>
<?php endforeach ?>
<?php Html::a('Прочитать все напоминание', ['#']) ?>
</div>
Answer the question
In order to leave comments, you need to log in
In foreach you are passing the $notification variable, but where is its initialization? This variable must contain an array of objects. And it comes, apparently, null.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question