這篇文章給大家介紹使用Yii怎么對(duì)行下的每列數(shù)據(jù)進(jìn)行遍歷,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
為涿州等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及涿州網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站、涿州網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!具體如下:
效果圖如下:
控制器(1種):
//顯示列表 public function actionList() { //實(shí)例化對(duì)象 $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(); //找出對(duì)應(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]); }
視圖層:
關(guān)于使用Yii怎么對(duì)行下的每列數(shù)據(jù)進(jìn)行遍歷就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。