Yii遍历行下每列数据的方法

时间:2021-05-02

本文实例讲述了yii遍历行下每列数据的方法。分享给大家供大家参考,具体如下:

效果图如下:

控制器(1种):

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 //显示列表 public function actionlist() { //实例化对象 $model= new qiu(); $country = \yii::$app->db; //查询数据 $data = $country->createcommand("select * from qiu join region on qiu.region_id=region.region_id")->queryall(); $region_ids = $country->createcommand("select region_id from region")->queryall(); $region = $country->createcommand("select * from region")->queryall(); //遍历数组 $ids = array(); $names = array(); $count = array(); //遍历区域id foreach ($region_ids as $key => $v) { $ids[$key] = $v['region_id']; } //print_r($ids);die; //遍历球队 foreach ($ids as $key => $val) { $data =qiu::find()->where(['region_id'=>$val])->asarray()->all(); $count[]=count($data); $rows[$val] = $data; } //print_r($rows);die; //根据所有记录进行遍历,显示最多行数 $ji = max($count); $arr = array(); //找出对应的球队 for($i=0;$i<$ji;$i++) { foreach($rows as $key => $val) { if(isset($val[$i])) { $arr[$i][$key] = $val[$i]['q_name']; } else { $arr[$i][$key] = ''; } } } //var_dump($arr);die; //分配数据 return $this->render('list',['arr'=>$arr,'region'=>$region]); }

(2种):

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public function actionlist1() { //实例化模型层 $region = new region; $qiu = new qiu; //取出区域表的iqiud和所有数据,队表数据 $region_ids = $region->find()->select('region_id')->column(); $areas = $region->find()->asarray()->all(); $team = $qiu->find()->asarray()->all(); $count = array(); $info = array(); foreach ($region_ids as $aid) {//1,2,3--6 foreach ($team as $key=>$val) { if($val['region_id'] == $aid){ $info[$aid][] = $val; $count[]=count($info[$aid]); } } } //var_dump($count);die; $con = max($count); $arr = array(); for ($i=0; $i <$con ; $i++) { foreach ($info as $key => $val) { if(isset($val[$i])){ $arr[$i][$key] = $val[$i]['q_name']; } else { $arr[$i][$key] = ''; } } } //var_dump($arr);die; return $this->render('list',['arr'=>$arr,'region'=>$areas]); }

视图层:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <table border="1"> <!--一行区域--> <tr style="background:red;"> <?php foreach ($region as $key => $v1) {?> <td><?php echo $v1['region_name']; ?></td> <?php }?> </tr> <!--每列球队--> <?php foreach ($arr as $key => $val) {?> <tr> <?php foreach ($val as $key => $v) {?> <td><?php echo $v; ?></td> <?php } ?> </tr> <?php } ?> </table>

希望本文所述对大家基于yii框架的php程序设计有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章