-1

I am getting this error,

Invalid argument supplied for foreach(); Undefined variable: rows........error in view line no 13.....

pls help me..i dont know wher is the error

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 Answer 1

1

in your model, return $q->result() instead of looping through and putting the rows in an array.

actually you can pretty much cut it down to this:

new function:

function cusall()       
{
    $q = $this->db->get('customer');
    return $q->result();
}
3
  • edited. this way it either returns a result object that your foreach will loop through, or a false which will never let the foreach trgger
    – Rooster
    Jun 22, 2012 at 19:25
  • you shouldnt need to change the view at all.
    – Rooster
    Jun 22, 2012 at 19:29
  • replace your model function with the one i wrote. you can verify its retrieving things from the db by using var_dump($data['rows']);exit; in your controller before you load the view
    – Rooster
    Jun 22, 2012 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.