-2

无法解决如何每 4 次获得最后一个迭代行,但我需要找到最后一个,这样我才能让它做一些事情

foreach( $gallery->getMedia() as $key => $media )
{
        if( ($key % 4 ) == 0 ) $html .= '<div class="clear"></div>';
        $url = $media->dir . '/' . $media->filename;

        $html .= sprintf( '<li><a href="#photo-%s" class="album_image_wrapper" id="photo_%s" title="%s" alt="%s" target="_blank" class="image-link" rel="slideshow"><div style="background-image: url(\'',
                $media->id ,
                $media->id ,
                $url,
                $gallery->name ,
                $media->id ,
                $this->generateThumbUrl( $url, array( 'width' => 100, 'height'=> 120, 'zoom' => true) ),
                $media->title,
                $galleryID  );
4

1 回答 1

2

If you're using Symfony2 then you definitely shouldn't be rendering HTML content like this, you should be using the templating engine, Twig.

e.g. From a controller (I assume this is in a controller):

return $this->render('BundleName:ControllerName:view.html.twig', array(
    'gallery' => $gallery
);

Then in your /path/to/BundleName/Resources/views/ControllerName/view.html.twig template:

{% for key, media in gallery.media %}
    {% if loop.last %}
        {# Logic for the last iteration of the loop #}
    {% endif %}

    {% if loop.index % 4 == 0 %}
        {# Logic for every 4th iteration #}
    {% endif %}

    {# other code... #}

{% endfor %}
于 2012-09-13T16:15:46.040 回答