Answer the question
In order to leave comments, you need to log in
How to output data correctly, yii2?
Hi all!
Two tables: country (id, country_name) and mark (id, country, mark).
record example:
country: 1, russia
mark: 1, 1 (country.id), lada
Output:
<?php foreach ($countrys as $country): ?>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<?= Html::encode("{$country->country_name}") ?><span class="caret"></span></a>
<ul class="dropdown-menu">
<li>
<a href="#"><?= Mark::menu($country->id) ?></a>
</li>
</ul>
</li>
<?php endforeach; ?>
class Mark extends ActiveRecord
{
public static function menu($id)
{
$query = self::find();
$mark = $query->where(['country' => $id])->all();
$mass = array();
foreach ($mark as $myMark) {
$mass[] = $myMark->mark;
}
return $mass;
}
}
public function actionIndex()
{
$query = Country::find();
$countrys = $query->orderBy('country_name')
->all();
return $this->render('index', [
'countrys' => $countrys,
]);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question