Codeigniter:模型foreach传递数组到控制器 - >查看

时间:2014-09-15 09:36:13

标签: php arrays codeigniter

这是我的模型 - 我想在视图页面上分配行和回显,例如

我的模特:

public function dsduan(){

       $data   =   array();
       $q = $this->db->query("SELECT duan.id, duan.tenduan, duan.diachi, quan.quan, duan.tdt, duan.mdxd, duan.tt, duan.cdt, duan.dvql, duan.dvtk, duan.dvtc, duan.dvgs, duan.loai, duan.tienich, duan.noidung, duan.hinhanh FROM duan LEFT JOIN quan ON duan.quan=quan.id ORDER BY duan.tenduan ASC");

        if ($q->num_rows == 1)
        { 
            foreach ($q->result() as $row)
            {                  
                $data[]['id'] = $row->id; 
                $data[]['tenduan'] = $row->tenduan;
                $data[]['diachi'] = $row->diachi;
                $data[]['quan'] = $row->quan;                   
            }
        }

        return $data; 
}


我的控制器:

public function dsduan() {

     $this->load->model('madmin');
     $duan = $this->madmin->dsduan();
     $this->load->view('danhsachduan', $duan);        
}


我的观点:

<td><?php echo $id; ?></td>
<td><?php echo $tenduan; ?></td>
<td><?php echo $diachi; ?></td>
<td><?php echo $quan; ?></td>

如何正确地将我的模型阵列传递给控制器​​,以便我可以回显我的视图? 我的观点错误:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: id

Filename: views/danhsachduan.php

Line Number: 44

5 个答案:

答案 0 :(得分:0)

在你的模型中,你使用二维数组:

  

$ data [] ['id'] = $ row-&gt; id;

然后在您的控制器中传递此数组以进行查看。

因此,如果要在视图中访问二维数组,则必须使用foreach循环。

例如在模型中更改数组,如下所示:

  

$ data ['id'] [] = $ row-&gt; id;

并在您的视图中使用以下访问数组:

foreach($id as $res)
{
   echo $res;
}

答案 1 :(得分:0)

修改您的代码。请使用您的代码替换

<强>型号:

public function dsduan()
{
     $q = $this->db->query("SELECT duan.id, duan.tenduan, duan.diachi, quan.quan, duan.tdt, duan.mdxd, duan.tt, duan.cdt, duan.dvql, duan.dvtk, duan.dvtc, duan.dvgs, duan.loai, duan.tienich, duan.noidung, duan.hinhanh FROM duan LEFT JOIN quan ON duan.quan=quan.id ORDER BY duan.tenduan ASC");

     if($q->num_rows() > 0)
         return $q->result();
     return false;
}


控制器:

public function dsduan() 
{    
     $this->load->model('madmin');
     $data['duan'] = $this->madmin->dsduan();
     $this->load->view('danhsachduan', $data);        
}


查看

<?php foreach($duan as $d) { ?>
    <td><?php echo $d->id; ?></td>
    <td><?php echo $d->tenduan; ?></td>
    <td><?php echo $d->diachi; ?></td>
    <td><?php echo $d->quan; ?></td>
<?php } ?>

答案 2 :(得分:0)

在codeigniter中,它将变量从控制器绑定到视图时使用extract()方法。在这种情况下,你可以这样做。

在模型中

public function dsduan()
{

       $data   =   array();
       $q = $this->db->query("SELECT duan.id, duan.tenduan, duan.diachi, quan.quan, duan.tdt, duan.mdxd, duan.tt, duan.cdt, duan.dvql, duan.dvtk, duan.dvtc, duan.dvgs, duan.loai, duan.tienich, duan.noidung, duan.hinhanh FROM duan LEFT JOIN quan ON duan.quan=quan.id ORDER BY duan.tenduan ASC");

       if ($q->num_rows)
       {
            $data = $q->result_array();
       }

        return $data; 
}

在控制器中

public function dsduan()
{
        $this->load->model('madmin');
        $data['duan'] = $this->madmin->dsduan();
        $this->load->view('danhsachduan', $data);

}

在视图中,如果你的数组是多维的,你可以这样使用,

<?php foreach($duan as $d) { ?>
   <td><?php echo $d['id']; ?></td>
   <td><?php echo $d['tenduan']; ?></td>
   <td><?php echo $d['diachi']; ?></td>
   <td><?php echo $d['quan']; ?></td>
<?php } ?>

如果你的数组是一维的,你可以这样使用,

   <td><?php echo $duan['id']; ?></td>
   <td><?php echo $duan['tenduan']; ?></td>
   <td><?php echo $duan['diachi']; ?></td>
   <td><?php echo $duan['quan']; ?></td>

答案 3 :(得分:0)

像这样更新你的观点

<?php foreach($duan as $d) { ?>
    <td><?php echo $d['id']; ?></td>
    <td><?php echo $d['tenduan']; ?></td>
    <td><?php echo $d['diachi']; ?></td>
    <td><?php echo $d['quan']; ?></td>
<?php } ?>

答案 4 :(得分:0)

public function dsduan(){

   $data   =   array();
   $q = $this->db->query("SELECT duan.id, duan.tenduan, duan.diachi, quan.quan, duan.tdt, duan.mdxd, duan.tt, duan.cdt, duan.dvql, duan.dvtk, duan.dvtc, duan.dvgs, duan.loai, duan.tienich, duan.noidung, duan.hinhanh FROM duan LEFT JOIN quan ON duan.quan=quan.id ORDER BY duan.tenduan ASC");

    if ($q->num_rows == 1)
    { 
        foreach ($q->result() as $row)
        {                  
            $data['id'] = $row->id; 
            $data['tenduan'] = $row->tenduan;
            $data['diachi'] = $row->diachi;
            $data['quan'] = $row->quan;                   
        }
    }

    return $data; 
}