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

#1
[eluser]mpar612[/eluser]
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:
Code:
public function view($slug)
{
  
  $data['artist'] = $this->artist_model->get_artist($slug);

  if(empty($data['artist']))
  {
   show_404();
  }

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

I have this function in my model:
Code:
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();
}

#2
[eluser]TheFuzzy0ne[/eluser]
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.

#3
[eluser]alvaroeesti[/eluser]


show the code of the View

#4
[eluser]CroNiX[/eluser]
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-2019 MyBB Group.