Passing array back from controller
Permalink
Hi
I'm missing something here, I've select some contents out of my database table which is coming back and being passed into an array, I need to pass this back to the view and then loop through it to display the contents of the array but the foreach in the view doesn't work.
controller
Then in the view
Not sure if its something to do with the $controller bit, I couldnt get myfunction in the controller to be called without it.
I'm missing something here, I've select some contents out of my database table which is coming back and being passed into an array, I need to pass this back to the view and then loop through it to display the contents of the array but the foreach in the view doesn't work.
controller
public function getSmallRNS($num){ $db = Loader::db(); Database::setDebug(true); $res = $db->Execute('select rns_id, title, unix_timestamp(time) as time from btrnsFeed order by time desc limit 0,'.$num); $rns = array(); while($my_data = $res->FetchRow()){ $rns[] = $my_data; } $this->set('my_data', $rns); //print_r($rns); }
Then in the view
Not sure if its something to do with the $controller bit, I couldnt get myfunction in the controller to be called without it.
You may find this howto useful:
http://www.concrete5.org/documentation/how-tos/developers/concrete5...
http://www.concrete5.org/documentation/how-tos/developers/concrete5...
Sorry that was me doing a typo I already have
But I get
Warning: Invalid argument supplied for foreach()
in the view :(
foreach($mydata as $val){
But I get
Warning: Invalid argument supplied for foreach()
in the view :(
Looking in more detail, you are calling getSmallRNS from the view and that is too late for set() to work.
Maybe change it so getSmallRNS returns the array $rns.
Then in the view:
Maybe change it so getSmallRNS returns the array $rns.
public function getSmallRNS($num){ $db = Loader::db(); Database::setDebug(true); $res = $db->Execute('select rns_id, title, unix_timestamp(time) as time from btrnsFeed order by time desc limit 0,'.$num); $rns = array(); while($my_data = $res->FetchRow()){ $rns[] = $my_data; } return $rns; }
Then in the view:
$my_data = $controller->getSmallRNS($number_to_show)
OK I've changed that now.
so in my view I have this:
But in the view I get all the array printed out via the print_r call
The echo sizeof returns 0 and then I still get the foreach error.
http://wallace.design-portfolio.info/concrete5demo.dp-online.co.uk/...
so in my view I have this:
But in the view I get all the array printed out via the print_r call
The echo sizeof returns 0 and then I still get the foreach error.
http://wallace.design-portfolio.info/concrete5demo.dp-online.co.uk/...
my bad! fat fingers and lack of underscores in the last bit, you previous answer has worked! thank you!
should be