Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php symfony 2中的foreach_Php_Foreach - Fatal编程技术网

Php symfony 2中的foreach

Php symfony 2中的foreach,php,foreach,Php,Foreach,在计算如何每4行获取最后一个迭代行时遇到了困难,它将放入a,但我需要找到最后一行,这样我就可以让它做一些事情 foreach( $gallery->getMedia() as $key => $media ) { if( ($key % 4 ) == 0 ) $html .= '<div class="clear"></div>'; $url = $media->dir . '/' . $media->filenam

在计算如何每4行获取最后一个迭代行时遇到了困难,它将放入a,但我需要找到最后一行,这样我就可以让它做一些事情

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  );
foreach($gallery->getMedia()作为$key=>$media)
{
如果(($key%4)==0)$html.='';
$url=$media->dir.'/'.$media->filename;

$html.=sprintf('
  • 如果您使用的是Symfony2,那么您绝对不应该像这样呈现html内容,您应该使用模板引擎

    e、 g.来自控制器(我假设这是在控制器中):

    然后在
    /path/to/BundleName/Resources/views/ControllerName/view.html.twig
    模板中:

    {% 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 %}
    
    {% 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 %}