Welcome Guest, Not a member yet? Register   Sign In
What am I doing wrong? DB, select, array
#1

[eluser]ZioN[/eluser]
I want to pass information from a database table to a view, but I cannot get it to pass over the result.

Controller (acp):
Code:
public function get_users(){
   $this->load->model('users_model');
  
   if ($this->users_model->get_users_model()){
   print_r ($data);
   //redirect('acp/users/manage_users', $test);
   }
   else{
//
   }
  }

Model (users_model):
Code:
public function get_users_model(){
  $query = $this->db->get('users');
  
  if ($query){ // if succesful
  $data = $query->result();
  return $data;
  }
  else{ // if not successful
   return false;
  }
}

A print_r (to test if the $data contains variables) on the model works, but it will not give the data back to the controller and thus not to the view.
#2

[eluser]mejlo[/eluser]
replace
Code:
if ($this->users_model->get_users_model()){
with
Code:
if ($data = $this->users_model->get_users_model()){
#3

[eluser]ZioN[/eluser]
Almost perfect. I tried sending it to the view but it does not recognize it.

Controller:
Code:
public function get_users(){
   $this->load->model('users_model');
  
   if ($data = $this->users_model->get_users_model()){
   $this->load->view('acp/manage_users_view', $data);
   }
   else{   }
  }
Model:
Code:
public function get_users_model(){
  $query = $this->db->get('users');
  
  if ($query){ // if succesful
  $data = $query->result();
  return $data;
  }
  else{ // if not successful
   echo "Unable to get data";
   return false;
  }
}

Error:
Code:
A PHP Error was encountered



Severity: Notice


Message:  Undefined variable: data


Filename: acp/manage_users_view.php


Line Number: 53
#4

[eluser]mejlo[/eluser]
try:
Code:
$this->load->view('acp/manage_users_view', array('data' => $data));
#5

[eluser]ZioN[/eluser]
That fixed it. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB