Home >php教程 >PHP开发 >How to traverse data in each column under a row in Yii

How to traverse data in each column under a row in Yii

高洛峰
高洛峰Original
2016-12-20 15:12:361136browse

The example in this article describes how Yii traverses the data in each column under the row. Share it with everyone for your reference, the details are as follows:

The renderings are as follows:

How to traverse data in each column under a row in Yii

Controller (1 type):

//显示列表
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][&#39;q_name&#39;];
        }
        else
        {
          $arr[$i][$key] = &#39;&#39;;
        }
      }
    }
    //var_dump($arr);die;
    //分配数据
    return $this->render(&#39;list&#39;,[&#39;arr&#39;=>$arr,&#39;region&#39;=>$region]);
}

(2 types):

public function actionList1()
{
    //实例化模型层
    $region = new Region;
    $qiu = new Qiu;
    //取出区域表的iQiud和所有数据,队表数据
    $region_ids = $region->find()->select(&#39;region_id&#39;)->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[&#39;region_id&#39;] == $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][&#39;q_name&#39;];
        } else {
          $arr[$i][$key] = &#39;&#39;;
        }
      }
    }
    //var_dump($arr);die;
    return $this->render(&#39;list&#39;,[&#39;arr&#39;=>$arr,&#39;region&#39;=>$areas]);
}

View layer:

<table border="1">
<!--一行区域-->
<tr style="background:red;">
<?php foreach ($region as $key => $v1) {?>
<td><?php echo $v1[&#39;region_name&#39;]; ?></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>

I hope this article will be helpful to you Our PHP programming based on the Yii framework is helpful.

For more related articles on Yii’s method of traversing data in each column under a row, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn