效果如上图所示: 控制器层 public function actionLists(){ $model = new YueModel; $country = \Yii::$app->db; $o_ids = $country->createCommand("select o_id from orea")->queryAll(); $ids = array(); $names = array(); $count = array(); foreach ($o_ids as $key => $v) { $ids[$key] = $v[‘o_id‘]; } foreach ($ids as $key => $val) { $data =$model->find()->where([‘o_id‘=>$val])->asArray()->all(); $count[]=count($data); $rows[$val] = $data; } $con = max($count); $team = array(); for ($i=0; $i <$con ; $i++) { foreach ($rows as $key => $val) { if(isset($val[$i])){ $team[$i][$key] = $val[$i][‘y_name‘]; } else { $team[$i][$key] = ‘‘; } } } //var_dump($team);//die; return $this->render(‘lists‘,[‘team‘=>$team,‘orea‘=>$orea]); } 视图层 <div> <table border="1"> <tr> <?php foreach($orea as $k=>$v){ ?> <th style="width:150px"><?php echo $v[‘o_name‘] ?></th> <?php } ?> </tr> <?php foreach ($team as $key => $val) {?> <tr> <?php foreach ($val as $key => $v) {?> <td><?php echo $v; ?></td> <?php } ?> </tr> <?php } ?> </table> </div>
时间: 2024-11-03 20:56:51