php - How to display data from tables and queries for each line of data from another table in Yii2 -


i have 2 tables - animals , animals_type.

columns in animals_type: id , type

columns in animals: id, name , animals_type

i want render list of animals type , exists animals on same page.

for render animals type create model animals_type, controller animals_typecontroller

<?php namespace frontend\controllers;  use yii\web\controller; use yii\data\pagination; use app\models\animals_type;  class animals_typecontroller extends controller {     public function actionindex()     {         $query = animals_type::find();          $pagination = new pagination([             'defaultpagesize' => 10,             'totalcount' => $query->count(),         ]);          $animals_type = $query->orderby('id')             ->offset($pagination->offset)             ->limit($pagination->limit)             ->all();          return $this->render('index', [             'animals_type' => $animals_type,             'pagination' => $pagination,         ]);     } } 

and view index.php

<?php use yii\helpers\html; use yii\widgets\linkpager;  ?>     <h1>animals type</h1>     <ul>         <?php foreach ($animals_types $animals_type):?>             <li>                 <?= html::encode("{$animals_type->type} ({$animals_type->id})") ?>:             </li>         <?php endforeach; ?>     </ul>  <?= linkpager::widget(['pagination' => $pagination]) ?> 

how right query list of animals each type?

after creatring relation animals can use somehow this:

<?php foreach ($animals_types $animals_type):?>     <li>         <?= html::encode("{$animals_type->type} ({$animals_type->id})") ?>:     <?if($animals_type->animals):?>         <ul>             <?php foreach ($animals_type->animals $animal):?>                 <?= html::encode("{$animal->name} ({$animal->id})") ?>:             <?php endforeach; ?>         </ul>     <?endif?>     </li> <?php endforeach; ?> 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -