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

效果图如下:

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

控制器(1种):

//显示列表
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种):

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]);
}

视图层:

<table border="1">
<!--一行区域-->
<tr style="background:red;">
<"_blank" href="https://www.jb51.net/Special/386.htm">Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

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

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!