• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Data not passing from controller to view

Hi everyone, I have done this dozens of times and am totally stumped as to why it isn't working.

For some reason the $data from my controller is not being made available to my view. If I do print_r($data) in my controller, it returns all of the data appropriately. If I access $artist in my view I get an error - Undefined variable: data

I am still pretty new to Codeigniter. Any thoughts or suggestions on places to start looking? Thanks!

I have this function in my controller:
public function view($slug)
  $data['artist'] = $this->artist_model->get_artist($slug);


  $this->load->view('artists/view', $data);

I have this function in my model:
public function get_artist($slug = FALSE)
  if($slug === FALSE)
   $query = $this->db->get('artists');
   return $query->result_array();
  $query = $this->db->get_where('artists', array('slug' => $slug));
  return $query->row_array();

What I find strange is that you're accessing $artist from within your view, but the error says the $data variable is undefined. You must be trying to access the $data variable within your view somewhere, and I think that's where the error lies.


show the code of the View

If you are sending $data['artist'] to your view, then in your view that variable is accessible by $artist, not $data['artist']. CI runs extract() on the $data array when sending it to the view.

If you want to still have the $data array defined (perhaps you want to group related data), then you need to send $data['data']['artist'] to the view and then $data['artist'] would be available.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.