Invalid argument supplied for foreach() in Codeigniter

前端 未结 3 1308
[愿得一人]
[愿得一人] 2021-01-07 07:41

I am getting error message: Invalid argument for foreach() in my View. I wanted to display all entries in my mysql table but i kept on getting error message. I am a newbie i

3条回答
  •  [愿得一人]
    2021-01-07 08:33

    you need to do a check before starting to iterate for the data, like: model code:

    public function getAll() {
        $results = array();
        $this->db->select('bcode, bname, btel, badd');
        $this->db->from('branches');
    
        $query = $this->db->get();
    
        if($query->num_rows() > 0) {
            $results = $query->result();
        }
        return $results;
    }
    

    view code:

    if( !empty($results) ) {
        foreach($results as $row) {
            echo '';
            echo ''.$row->bcode.'';
            echo ''.$row->bname.'';
            echo ''.$row->btel.'';
            echo ''.$row->badd.'';
            echo '';
        }
    }
    

提交回复
热议问题