我正在尝试在我的控制器中创建if else但是不知道id怎么能这样做我的控制器和菜单代码在那里给出 我的要求是关于页面如何显示所有3个quires但是当我尝试时我只能逐个显示我想要这样做因为我只想在同一页面中按组显示文档所以这可能吗?
这是我的控制器
public function actionAbout($id = null){
// join with other table by province
if ($id == '15' ) {
$query = bio::find()
->joinWith('wat')
->where(['wat.province' => $id ]);
}
// join with other table by amphur
if ($id == array('178, 179, 180, 181, 182, 183')) {
$query = bio::find()
->joinWith('wat')
->where(['wat.amphur' => $id ]);
}else{
// ///////// like menu list //////////
$query = Bio::find()
->where(['LIKE', 'rolerule', $id]);
}
// $query = Bio::find()->orderBy('bio_id DESC')->where(['status' => 1]);
$countQuery = clone $query;
$pages = new Pagination(['defaultPageSize' => 30 ,'totalCount' => $countQuery->count()]);
$models = $query->offset($pages->offset)
->limit($pages->limit)
->all();
return $this->render('about', [
'records' => $models,
'pages' => $pages,
]);
}
这是我的菜单
<a href="#demo2" class="list-group-item list-group-item-info" data-toggle="collapse" data-parent="#MainMenu">provience</a>
<div class="collapse" id="demo2">
<a href="about?id=178" class="list-group-item"> provience </a>
</div>
<a href="#demo3" class="list-group-item list-group-item-info" data-toggle="collapse" data-parent="#MainMenu">Amphur</a>
<div class="collapse" id="demo3">
<a href="about?id=178" class="list-group-item"> Amphur </a>
<a href="about?id=179" class="list-group-item"> muang</a>
<a href="about?id=180" class="list-group-item"> test1</a>
<a href="about?id=181" class="list-group-item"> test2</a>
<a href="about?id=182" class="list-group-item"> test3</a>
<a href="about?id=183" class="list-group-item"> test4</a>
</div>
<a href="#demo4" class="list-group-item list-group-item-info" data-toggle="collapse" data-parent="#MainMenu">hero</a>
<div class="collapse" id="demo4">
<a href="about?id=hero" class="list-group-item">hero</a>
<a href="about?id=superman" class="list-group-item">superman</a>
<a href="about?id=batman" class="list-group-item">batman</a>
<a href="about?id=ironman" class="list-group-item">ironman</a>
</div>
答案 0 :(得分:1)
您只获得最后一个查询,因为您重新分配了
假设您可以针对you_value测试$ your_test_var,这可能是一个建议
public function actionAbout($id = null){
// join with other table by province
if ($your_test_var == 'your_first_val' ) {
$query = bio::find()
->joinWith('wat')
->where(['wat.province' => $id ]);
}
// join with other table by amphur
if ($your_test_var == 'your_second_val' ) {
$query = bio::find()
->joinWith('wat')
->where(['wat.amphur' => $id ]);
} else {
// ///////// like menu list //////////
$query = Bio::find()
->where(['LIKE', 'rolerule', $id]);
}
// $query = Bio::find()->orderBy('bio_id DESC')->where(['status' => 1]);
$countQuery = clone $query;
$pages = new Pagination(['defaultPageSize' => 30 ,'totalCount' => $countQuery->count()]);
$models = $query->offset($pages->offset)
->limit($pages->limit)
->all();
return $this->render('about', [
'records' => $models,
'pages' => $pages,
]);
}