Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions

Passing an array to a template


Reply

2 Posts   206 Views

Avatar
tahbit

12 June 2014 at 4:16am Community Member, 24 Posts

Guys - this is very simple - I have an array - see below. All I want to do is pass the data from the DB to a template and display it on the screen. Once I have understood how to do that I'll be away... The code below works fine in the controller - I can echo it - var_dump it you name it!!!

BUT I cannot get the stuff in a view - I just need someone to show me an example both in the controller and in the template.

<code>
public function getStudent()
   {
      
      $members = Student::get()->where("\"Name\" = 'Sarah Berens'");

      foreach($members as $member)
      {
       $data = array('Name' => $member->Name);

      
      }

      var_dump($data);
      //return $data;
      //($members);
   }

</code>

I have thrashed around for hours trying to do this - poking and hoping - all sorts. But no good - Please help!! Thanks

Avatar
tahbit

12 June 2014 at 6:25am Community Member, 24 Posts

Right - cracked it! In case there is anybody else in a similar fix - here we go...

Controller:

$students = new ArrayList();

foreach($members as $member)
{

$students->push((new ArrayData(array(
'File' => $member->Name
))));
}

And for the view:

<% loop Student %>
<p>$File</p>
<% end_loop %>

Can't believe it took me so long - but there we go... Hope it helps!