0

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

9
  • are you sure you are using correct view? Commented Sep 7, 2018 at 12:48
  • Yes I am, I am loading a correct view Commented Sep 7, 2018 at 12:49
  • go in you view and dump like this var_dump($this->_ci_cached_vars); tell me what are you getting there Commented Sep 7, 2018 at 12:51
  • @SyedArifIqbal Got this array (size=0) empty Commented Sep 7, 2018 at 12:54
  • try something like this if this works $this->load->view('dashboard', ['u_list' => $this->userinsert->select()]); Commented Sep 7, 2018 at 12:56

4 Answers 4

2

Replace your function select with below code

public function select() {  
    $query = $this->db->get('users');
    return $query->result(); 
}  

Hope this helps

Sign up to request clarification or add additional context in comments.

1 Comment

Try to debug by print_r($data['u_list']); exit; in your controller
1

You forgot result in MODEL

 public function select() {  
        $query = $this->db->get('users')->result();
        return $query; 
    } 

6 Comments

just print $query and tell em what you getting?
Where? in view part?
check in model part
Checked the model part nothing printed by $query
So now just echo $this->db->last_query(); die; before return. and use that printed query in your mysql DB.
|
1
// 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>

2 Comments

Same error Message: Undefined variable: data and Message: Invalid argument supplied for foreach()
public function select() { $query = $this->db->get('users'); $query = mysqli_fetch_all($query ); return $query; }
0

in your model ...

public function select() {  
      $this->db->select("*"); 
      $this->db->from('users');
      $query = $this->db->get();
      return $query->result();
}

1 Comment

public function select() { $this->db->select("*"); $this->db->from('users'); $query = $this->db->get(); if ($query->num_rows() >0 ) { return $query->result(); } }

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.