Outputting array results from Controller

Permalink
Hello in my controller i have a view function that runs a query to get data from a db. I have the data and now need to output it with something like a foreach loop within my view state on my single page.

In the controller view state i have

public function view() {   
         $db = Loader::db();
         $data = $db->getAll("SELECT * FROM btCpd");
         $this->set('data', $data);
   }


Can any one advise how i take the data from this function and display it in my view,php..?

 
ScottSandbakken replied on at Permalink Reply
ScottSandbakken
foreach ($data as $col => $val) {
   echo '<br/>' . $col . ': ' . $val;
}
obaudains replied on at Permalink Reply
THANKS AGAIN FOR YOUR HELP NETJUNKY.

I HAVE PASTED THE CODE YOU PROVIDED IN MY VIEW.PHP. This has displayed the following text:

0: Array
1: Array
2: Array
3: Array
4: Array
5: Array
6: Array

I need to display items for each...e.g. title, hours date etc.
Can you explain how i map the database column names to achieve this please.

(Thank you for your patience)
ScottSandbakken replied on at Permalink Best Answer Reply
ScottSandbakken
Try this:
echo '<hr/>'
foreach ($data as $row) {
   foreach ($row as $col => $val) {
      echo '<br/>' . $col . ': ' . $val;
   }
   echo '<hr/>'
}


or this

foreach ($data as $r) {
      echo '<br/>date_added: ' . $r['date_added'];
      echo '<br/>user_id: ' . $r['user_id'];
      echo '<br/>title: ' . $r['title'];
      echo '<br/>hours: ' . $r['hours'];
      echo '<br/>description: ' . $r['description'];
      echo '<br/>date_conducted: ' . $r['date_conducted'];
   echo '<hr/>'
}
obaudains replied on at Permalink Reply
awesome, thanks!