为foreach()提供的参数无效;

时间:2012-06-22 19:05:20

标签: php codeigniter

我收到此错误,

为foreach()提供的参数无效; 未定义的变量:行........视图行第13行中的错误.....

请帮助我......我不知道这是错误

1.controller

function customer_area() {

  $this->load->model('cus_model');

  $data['rows']=$this->cus_model->cusall();

  $this->load->view('customer_view',$data);

}

2.models

function cusall() {     
  $q = $this->db->get('customer');        
  $this->db->select('*');
  if($q->num_rows()>0) {    
    foreach($q->result() as $rows){      
      $data[]=$rows;
    }
    return $data;
  }
}`

3.views

<table border="1" height="200" width="200" bordercolor="#003366">
      <tr><th>ID</th></td><th>NAME</th><th>Type</th><th>Address</th><th>Mobile</th><th>Skype_Id</th><th>Email</th><th colspan="2">Actiom</th></tr>

      foreach($rows as $r)

    {
      echo "<tr>";
      echo "<td>". $r->id ."</td>";
      echo "<td>". $r->name ."</td>";
      echo "<td>". $r->type ."</td>";
      echo "<td>". $r->address ."</td>";
      echo "<td>". $r->mobile ."</td>";
      echo "<td>". $r->skype_id ."</td>";
      echo "<td>". $r->email ."</td>";
      echo "<td>". anchor('customer/input/'.$r->id,'Edit') ."</td>";    
      echo "<td>". anchor('customer/del/'.$r->id,'Delete') ."</td>";
      echo "</tr>"; 
      echo "<br>"; 
      }


      ?>
      </table>

1 个答案:

答案 0 :(得分:1)

在您的模型中,返回$ q-&gt; result()而不是循环并将行放入数组中。

实际上,你可以将其简化为:

新功能:

function cusall()       
{
    $q = $this->db->get('customer');
    return $q->result();
}