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
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

$controller->getSmallRNS($number_to_show);
     echo sizeof($my_data);
       foreach($>my_data as $row){
      echo $row['rns_id'];   
       }


Not sure if its something to do with the $controller bit, I couldnt get myfunction in the controller to be called without it.

designportfolio
 
JohntheFish replied on at Permalink Reply
JohntheFish
Typo
foreach($>my_data as $row)


should be
foreach($my_data as $row)
JohntheFish replied on at Permalink Reply
JohntheFish
designportfolio replied on at Permalink Reply
designportfolio
Sorry that was me doing a typo I already have

foreach($mydata as $val){


But I get

Warning: Invalid argument supplied for foreach()

in the view :(
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
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.
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)
designportfolio replied on at Permalink Reply
designportfolio
OK I've changed that now.

so in my view I have this:

$my_data = $controller->getSmallRNS($number_to_show);
print_r($my_data);
echo sizeof($mydata);
Log::addEntry($my_data);
foreach($mydata as $row){
   echo $row['rns_id'];   
}


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/...
designportfolio replied on at Permalink Reply
designportfolio
my bad! fat fingers and lack of underscores in the last bit, you previous answer has worked! thank you!