Yii遍歷行下每列數(shù)據(jù)的方法
本文實(shí)例講述了Yii遍歷行下每列數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
效果圖如下:
控制器(1種):
//顯示列表 public function actionList() { //實(shí)例化對象 $model= new Qiu(); $country = \Yii::$app->db; //查詢數(shù)據(jù) $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(); //遍歷數(shù)組 $ids = array(); $names = array(); $count = array(); //遍歷區(qū)域ID foreach ($region_ids as $key => $v) { $ids[$key] = $v['region_id']; } //print_r($ids);die; //遍歷球隊(duì) foreach ($ids as $key => $val) { $data =Qiu::find()->where(['region_id'=>$val])->asArray()->all(); $count[]=count($data); $rows[$val] = $data; } //print_r($rows);die; //根據(jù)所有記錄進(jìn)行遍歷,顯示最多行數(shù) $ji = max($count); $arr = array(); //找出對應(yīng)的球隊(duì) 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; //分配數(shù)據(jù) return $this->render('list',['arr'=>$arr,'region'=>$region]); }
(2種):
public function actionList1() { //實(shí)例化模型層 $region = new Region; $qiu = new Qiu; //取出區(qū)域表的iQiud和所有數(shù)據(jù),隊(duì)表數(shù)據(jù) $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]); }
視圖層:
<table border="1"> <!--一行區(qū)域--> <tr style="background:red;"> <?php foreach ($region as $key => $v1) {?> <td><?php echo $v1['region_name']; ?></td> <?php }?> </tr> <!--每列球隊(duì)--> <?php foreach ($arr as $key => $val) {?> <tr> <?php foreach ($val as $key => $v) {?> <td><?php echo $v; ?></td> <?php } ?> </tr> <?php } ?> </table>
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
- 解析yii數(shù)據(jù)庫的增刪查改
- Yii實(shí)現(xiàn)MySQL多數(shù)據(jù)庫和讀寫分離實(shí)例分析
- Yii2.0高級框架數(shù)據(jù)庫增刪改查的一些操作
- Yii實(shí)現(xiàn)多數(shù)據(jù)庫主從讀寫分離的方法
- YII2數(shù)據(jù)庫查詢實(shí)踐
- PHP的Yii框架中使用數(shù)據(jù)庫的配置和SQL操作實(shí)例教程
- Yii2如何批量添加數(shù)據(jù)
- Yii2 GridView實(shí)現(xiàn)列表頁直接修改數(shù)據(jù)的方法
- yii數(shù)據(jù)庫的查詢方法
- Yii2 輸出xml格式數(shù)據(jù)的方法
- Yii學(xué)習(xí)總結(jié)之?dāng)?shù)據(jù)訪問對象 (DAO)
- Yii2框架數(shù)據(jù)庫簡單的增刪改查語法小結(jié)
- Yii2基于Ajax自動(dòng)獲取表單數(shù)據(jù)的方法
相關(guān)文章
Yii實(shí)現(xiàn)的多級聯(lián)動(dòng)下拉菜單
這篇文章主要介紹了Yii實(shí)現(xiàn)的多級聯(lián)動(dòng)下拉菜單,包括視圖、模型及控制器的相關(guān)實(shí)現(xiàn)代碼,涉及基于Yii的數(shù)據(jù)庫查詢、數(shù)組遍歷與數(shù)據(jù)顯示等相關(guān)操作技巧,需要的朋友可以參考下2016-07-07TP3.2框架分頁相關(guān)實(shí)現(xiàn)方法分析
這篇文章主要介紹了TP3.2框架分頁相關(guān)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了TP3.2框架分頁相關(guān)步驟、功能實(shí)現(xiàn)方法與操作技巧,需要的朋友可以參考下2020-06-06