How to transfer array values ​​from a database to a variable? I want to make a managed menu for the administrator. On the frontend in layouts / main menu is written here

$menuItems = [ ['label' => $item1, 'url' => [$item2]], ]; 

I make a query to the database to get the data $query = Menu::find()->all(); I'm trying to pull

 foreach ($query as $index => $array) { $menuItems = [ ['label' => $item1, 'url' => [$item2]], ]; } 

But it displays only the last value in the database. Tried to pass a value on an array, like so, an error

 $item1 = []; $item2 = []; $query = Menu::find()->all(); foreach ($query as $index => $array) { $item1[$index]['label'] = $array['text']; $item2[$index]['url'] = $array['url']; } $menuItems = [ ['label' => $item1, 'url' => [$item2]], ]; 

And how to write ?? In the debugger you can see that the parameters are passed

in C: \ OpenServer \ domains \ localhost \ path \ frontend \ views \ layouts \ main.php at line 72 - yii \ base \ Widget :: widget (['options' => [' class' => 'navbar-nav navbar-right '],' items '=> [[' label '=> [[' label '=>' Album '], [' label '=>' About university '], [' label '=>' Contacts ']],' url '=> [[[' url '=>' category / '], [' url '=>' / about '], [' url '=>' / contact ']]]], '

  • But mistake

    htmlspecialchars () expects parameter 1 to be string, array given

    • Write clearly and easily what you get from the database, and then what you need to get from this is no less available, now you can only see your code, but the essence of the manipulations is not very clear - MasterAlex
    • Here is a screen base c2n.me/3AnGfcN From it, we get a list of text - the names of the menu links, and the links themselves - url. It is necessary to write these parameters in $ menuItems. By standard, it is written as - ['label' => 'Name', 'url' => ['Link']]. The question is how to write to the label and url data from the database? - Vlad
    • yeah, and what do you need to get, what view should the menu have? - MasterAlex
    • c2n.me/3AnHqku Here is the menu, where instead of Text1, Text2 - data from the database - Vlad
    • no problem at all :) it's the same standard output in Yii2 - MasterAlex

    2 answers 2

    Create a folder in the widgets (if this folder does not exist, then you need to create it in the root of the site) WMenu.php file:

     <?php namespace app\widgets; use app\models\Menu; use yii\bootstrap\Widget; class WMenu extends Widget { public function init(){} public function run() { return $this->render('menu/view', [ 'menuItems' => Menu::find()->all() ]); } } 

    Next, in the same folder, create the 'views' folder, if you haven’t had widgets before, and in the views folder, create the menu folder, the view.php file will be in this folder:

     <?php use yii\helpers\Url; ?> <ul> <?php foreach($menuItems as $item) { ?> <li><a href="<?= Url::to(['/'.$item->url]); ?>"><?= $item->text ?></a></li> <?php } ?> </ul> 

    Next, in the layouts/main.php file, layouts/main.php on top:

     use app\widgets\WMenu; 

    and in the right place inserts this code:

     <?= WMenu::widget(); ?> 

    That's all, in principle, this is the standard way to add widgets to the project, I highly recommend it to be mastered, it will come in handy more than once.

      UPD Maybe someone will need

      Here is the code

       //формируем массив для меню $query = Menu::find()->all(); $menuItems = []; foreach ($query as $index => $array) { $menuItems[$index]['label'] = $array['text']; $menuItems[$index]['url'] = $array['url']; } echo Nav::widget([ 'options' => ['class' => 'navbar-nav navbar-right'], 'items' => $menuItems, ]);