Quantcast

controller/view

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

controller/view

Thomas List
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: controller/view

weierophinney
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: controller/view

sina miandashti
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
(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




--
________________
Sincerely
Sina Miandashti
MuSicBasE.ir & InvisionPower.ir Admin
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: controller/view

                                                                 <?php endforeach ?>

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.
jsuggs
In reply to this post by weierophinney
weierophinney wrote
You could also pass them to a partial to render:

    <?php echo $this->partial('members', $house->members) ?>

and put the logic in your partial.
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);?>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: controller/view

Wil Moore III
weierophinney wrote
You could also pass them to a partial to render:
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
Loading...