|
I'm having trouble writing my array values to my view. In the past, I've used objects and $this->view->[object] = $object->fetchAll(), and in my view I've just had to do a foreach($this->object as obj), and echo out the values of the object. I've also only had to deal with an array value with one rowset in it, so I've just had to echo out the values in the array. Now I'm trying to send an array with multiple rows to the view, and it's just printing out the last value of the array equal to the size of the array. Is there a way to do this similar to the object method? For example:
(in my controller, IndexAction) $this->view->houses = $houses->fetchAll(); (in my index.phtml file, or view) <?php foreach($this->houses as $house) : ?> <tr> <td><?php echo $this->escape($house->types);?></td> <td><?php echo $this->escape($house->ownership);?></td> <td><?php echo $this->escape($house->income);?></td> <td><?php echo $this->escape($house->members);?></td> </tr> Any suggestions would be very helpful. Thomas |
|
Administrator
|
-- Thomas List <[hidden email]> wrote
(on Wednesday, 08 September 2010, 01:56 PM -0400): > I'm having trouble writing my array values to my view. In the past, I've used > objects and $this->view->[object] = $object->fetchAll(), and in my view I've > just had to do a foreach($this->object as obj), and echo out the values of the > object. I've also only had to deal with an array value with one rowset in it, > so I've just had to echo out the values in the array. Now I'm trying to send > an array with multiple rows to the view, and it's just printing out the last > value of the array equal to the size of the array. Is there a way to do this > similar to the object method? For example: > (in my controller, IndexAction) > $this->view->houses = $houses->fetchAll(); > > (in my index.phtml file, or view) > <?php foreach($this->houses as $house) : ?> > <tr> > <td><?php echo $this->escape($house->types);?></td> > <td><?php echo $this->escape($house->ownership);?></td> > <td><?php echo $this->escape($house->income);?></td> > <td><?php echo $this->escape($house->members);?></td> > </tr> I'm guessing from your description and the snippent above that one or more of the values may be array data and/or iterable? such as "types" and "members"? As such, you'll need to aggregate those values to build up the final output. As examples: * If they are enumerated arrays: <?php foreach($this->houses as $house) : $types = implode(', ', $house->types); $members = implode(', ', $house->members); ?> <tr> <td><?php echo $this->escape($types);?></td> <td><?php echo $this->escape($house->ownership);?></td> <td><?php echo $this->escape($house->income);?></td> <td><?php echo $this->escape($members);?></td> </tr> <?php endforeach ?> * If they are associative arrays or objects: <?php foreach($this->houses as $house) : // Assuming types is enumerative $types = implode(', ', $house->types); // Handling members $members = array(); foreach ($house->members as $member) { $members[] = $member->firstName . ' ' . $member->lastName; } $members = implode(', ', $members); ?> <tr> <td><?php echo $this->escape($types);?></td> <td><?php echo $this->escape($house->ownership);?></td> <td><?php echo $this->escape($house->income);?></td> <td><?php echo $this->escape($members);?></td> </tr> <?php endforeach ?> You could also pass them to a partial to render: <?php echo $this->partial('members', $house->members) ?> and put the logic in your partial. -- Matthew Weier O'Phinney Project Lead | [hidden email] Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc |
|
nice reply Matthew ... thanks
On Wed, Sep 8, 2010 at 10:48 PM, Matthew Weier O'Phinney <[hidden email]> wrote: -- Thomas List <[hidden email]> wrote -- ________________ Sincerely Sina Miandashti MuSicBasE.ir & InvisionPower.ir Admin |
|
In reply to this post by weierophinney
In general I favor partials (depending on the situation), simply because that logic is now re-usable. If you need to have that same list displayed on multiple pages you can simply call the partial. I also like to create view helpers to pass entities to so that they are consistently displayed across the site. <?php foreach($this->houses as $house) : <?php echo $this->printTypes($types);?> |
<?php echo $this->escape($house->ownership);?> |
<?php echo $this->escape($house->income);?> |
<?php echo $this->printMembers($members);?> |
That way, if you want to adjust how you display the types or members, you can change it in one place and your entire site remains consistent. |
Using the partialLoop helper will further help you DRY up your views. It essentially saves you from having to worry about the "foreach" part.
--
Wil Moore III Best Practices for Working with Open-Source Developers http://www.faqs.org/docs/artu/ch19s02.html Why is Bottom-posting better than Top-posting: http://www.caliburn.nl/topposting.html DO NOT TOP-POST and DO trim your replies: http://linux.sgms-centre.com/misc/netiquette.php#toppost |
| Powered by Nabble | Edit this page |
