Reputation: 415
I am trying to fetch data from a table, I created a controller, model and view but when I try to open view, I am getting two errors one is Message: Undefined variable: u_list
and another one is Message: Invalid argument supplied for foreach()
I am using CodeIgniter 3.1.9
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class UserFetch extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->database();
$this->load->model('userinsert');
}
public function index() {
$data['u_list']=$this->userinsert->select();
$this->load->view('dashboard', $data);
}
}
?>
Model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class UserInsert extends CI_Model {
function __construct() {
parent::__construct();
}
function user_insert($data) {
$this->db->insert('users', $data);
}
public function select() {
$query = $this->db->get('users');
return $query;
}
}
?>
View
<tbody>
<?php
foreach ($u_list as $row) {
?>
<tr>
<td><?php echo $row->first_name;?></td>
<td><?php echo $row->last_name;?></td>
</tr>
<?php }
?>
</tbody>
Help me with this guys
Upvotes: 0
Views: 308
Reputation: 56
// use this function in your module
public function select() {
$query = $this->db->get('users');
$query = mysqli_fetch_all($query );
return $query;
}
<tbody>
<?php
foreach ($data['u_list'] as $row) {
?>
<tr>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['last_name'];?></td>
</tr>
<?php }
?>
</tbody>
Upvotes: 1
Reputation: 1066
in your model ...
public function select() {
$this->db->select("*");
$this->db->from('users');
$query = $this->db->get();
return $query->result();
}
Upvotes: 0
Reputation: 315
Replace your function select with below code
public function select() {
$query = $this->db->get('users');
return $query->result();
}
Hope this helps
Upvotes: 2
Reputation: 5322
You forgot result
in MODEL
public function select() {
$query = $this->db->get('users')->result();
return $query;
}
Upvotes: 1