Welcome Guest, Not a member yet? Register   Sign In
Passing an array from controller to view
#1

[eluser]Dandy_andy[/eluser]
I'm fairly new to PHP and have a question on passing an array. In a library class, I have a function under which there is an array of options. I am doing things this way for a few reasons and do not wish to store options in the DB:-

Code:
function interests() {

  $interests[1]='interest1';
  $interests[2]='interest2';
  $interests[3]='interest3';
  $interests[4]='interest4';
  
  return $interests;
}

In my controller, I access the options by calling the function:-

Code:
$this->load->library('profileoptions');
$interests = $this->profileoptions->interests();

All is fine up until there and I can output the array from the controller without any problem. However, how do I pass the array from the controller to the view using a simple expression like:-

Code:
$data['interests'] = isset($interests);

so that I can either create a FOR loop or display all the data at once using the most simplest syntax?

If I try to extract all the values from the array in the viewer, they do not appear.

Any help would be appreciated.

Thanks
#2

[eluser]meer[/eluser]
try this
modify your library function
function interests() {

$data= array( 'interests1'=$interest1,
'interests2'=$interest2,
'interests3'=$interest3,
'interests4'=$interest4,);

return $data;
}
in your controller get this dta as

$interest['data']= $this->profileoptions->interests();
and pass this $interest in your view
$this->load->view('your_view',$interest);
#3

[eluser]Dandy_andy[/eluser]
Thanks for the reply. I modified the code similar to what you described but kept the array in similar format. It is working now. The issue was with how I was passing the information to the View. Thanks for your help.
#4

[eluser]meer[/eluser]
cheers:-)




Theme © iAndrew 2016 - Forum software by © MyBB