多维数据传递到CodeIgniter中的View页面

时间:2016-05-17 17:46:17

标签: php codeigniter

最近我遇到了一个项目问题。

控制器页面

 if ($result -> num_rows () ==1) {
        $info['stu_data']=array();
        $i=0;
        $rs= $this -> db -> query ($this -> User_model -> view_action());
        if ($rs -> num_rows() > 0) {
          foreach ($rs -> result() as $rows) {
            $info [$i]['stu_name'] = $rows -> stu_name;
            $info [$i]['username'] = $rows -> username ;
            $info [$i]['roll'] = $rows -> roll ;
            $info [$i]['password'] = $rows -> password ;
            $info [$i]['first'] = $rows -> first;
            $info [$i]['second'] = $rows -> second ;
            $info [$i]['third'] = $rows -> third;
            $info [$i]['fourth'] = $rows -> fourth;
            $info [$i]['fifth'] = $rows -> fifth ;
            $i++;

          }

        }

        //$data['data']=$this -> User_model -> view_action();
        //print_r($info);
       $this -> load -> view ('add',$info);
    }

视图页面是

<?php
var_dump($stu_data); 
?>

但是我在add.php页面中得到了Noting。但是在Controller页面上$ info变量的值是可以的。

请解决此问题。

2 个答案:

答案 0 :(得分:1)

你的fetch方法

$info['stu_data'] 未填充

从查询中提取结果时,请尝试$info ['stu_data']而不是$info

此外,正如Rocket Hazmat所述,您可以通过不使用辅助变量(在您的情况下为[$i])并动态创建数组来简化当前的获取过程。

<小时/> 优化代码

if ($rs -> num_rows() > 0) {
      foreach ($rs -> result() as $rows) {
        $info['stu_data'][] = array('stu_name' => $rows -> stu_name, 'username' => $rows -> username, 
        'roll' => $rows -> roll, 'password' = $rows -> password, 'first' => $rows -> first,
        'second' => $rows -> second, 'third' => $rows -> third, 'fourth' => $rows -> fourth, 
        'fifth' => $rows -> fifth);
      }

答案 1 :(得分:0)

使用此代码将数据传递到视图页面。它使用简单,易于理解。

if ($result -> num_rows () ==1) {
    $i=0;
    $rs= $this -> db -> query ($this -> User_model -> view_action());
    if ($rs -> num_rows() > 0) {
      foreach ($rs -> result() as $rows) {
        $data['stu_name'] = $rows -> stu_name;
        $data['username'] = $rows -> username ;
        $data['roll'] = $rows -> roll ;
        $data['password'] = $rows -> password ;
        $data['first'] = $rows -> first;
        $data['second'] = $rows -> second ;
        $data['third'] = $rows -> third;
        $data['fourth'] = $rows -> fourth;
        $data['fifth'] = $rows -> fifth ;
        $info['stu_data'][$i]=$data;
        $i++;
      }

    }

    //$data['data']=$this -> User_model -> view_action();
    //print_r($info);
   $this -> load -> view ('add',$info);
}

我希望你能得到解决方案。