2010年9月9日星期四

Re: [fw-mvc] controller/view

nice reply Matthew  ... thanks

On Wed, Sep 8, 2010 at 10:48 PM, Matthew Weier O'Phinney <matthew@zend.com> wrote:
-- Thomas List <thomaslist@hotmail.com> 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            | matthew@zend.com
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

没有评论: