Welcome Guest, Not a member yet? Register   Sign In
Returning Data From a Class (solved: pass $data to the method)
#1

[eluser]blackhalobender[/eluser]
Is there some way to append rather then re-assign?


in my class:
Code:
...
$detail[name] = $row->name;
$detail[body] = $row->body;
return detail;
...

In my controller:
Code:
...
$data['someStaticData'] = 'x';
$data = $this-Myclass->detail($id);
...

In my view:
Code:
<?=$someStaticData;?>


This is a simplified example, but should this work? Right now x isn't defined any longer once the view loads.
#2

[eluser]tonanbarbarian[/eluser]
firstly in your view the variable would be $someStaticData not $x
secondly because you are assigning the result of the method call to $data its value is overwritten
you could pass $data to the method and either add to it and return it, or pass it by reference

Code:
function detail($id, $data) {
  $detail[name] = $row->name;
  $detail[body] = $row->body;
  return detail;
} // detail()

Code:
$data['someStaticData'] = 'x';
$data = $this-Myclass->detail($id, $data);

OR

Code:
function detail($id, &$data) {
  $detail[name] = $row->name;
  $detail[body] = $row->body;
} // detail()

Code:
$data['someStaticData'] = 'x';
$this-Myclass->detail($id, $data);
#3

[eluser]blackhalobender[/eluser]
Thanks, I was just returning here to post the solution. But thanks, now I know it's an actual solution not a hack.




Theme © iAndrew 2016 - Forum software by © MyBB