dongzexi5125 2013-03-19 16:17
浏览 44
已采纳

foreach循环问题与自定义字段

I have this bootstrap slider that doesn't seem to be grabbing the video links properly.

This is the code I have:

        <?php $myCarousel = get_order_field('projectdetails_image'); // use the Custom Group name 
        if( !empty( $myCarousel )) {
                foreach($myCarousel as $carousel){
                $myimg = get('projectdetails_image',1,$carousel);
                    if ( !$myimg ) {
                    $videos = get_order_field('video_vimeo');
                    foreach($videos as $video){
                        if ( get('video_vimeo',TRUE) ) {
                                echo "<div class='item active black'><div id='video-wrap'>";
                                echo get('video_vimeo',1,$video);
                                echo "</div></div>";
                        }
                        else {}
                        }   
                    }
                    if ( $myimg ) {
                        echo "<div class='item".( $counter == 0 ? ' active' : '' )."'>";
                        echo '<img src="';
                        echo $myimg;
                        echo '"/>';
                        echo "</div>";
                        $counter++;

                        $videos = get_order_field('video_vimeo');
                        foreach($videos as $video){
                            if ( get('video_vimeo',TRUE) ) {
                                echo "<div class='item black'><div id='video-wrap'>";
                                echo get('video_vimeo',1,$video);
                                echo "</div></div>";
                            }
                        }
                    }
                }
            }
            ?>

The bottom if statement is giving me problems:

if ( $myimg ) {
                    echo "<div class='item".( $counter == 0 ? ' active' : '' )."'>";
                    echo '<img src="';
                    echo $myimg;
                    echo '"/>';
                    echo "</div>";
                    $counter++;

                    $videos = get_order_field('video_vimeo');
                    foreach($videos as $video){
                        if ( get('video_vimeo',TRUE) ) {
                            echo "<div class='item black'><div id='video-wrap'>";
                            echo get('video_vimeo',1,$video);
                            echo "</div></div>";
                        }
                    }
                }

For some reason its outputting a <div class='item black'> after every <div class='item'> whereas I would like it to output the <div class='item black'> only after all the <div class='item'>'s have been outputted.

I hope that makes sense. If someone could just point me in the right direction that would be great.

  • 写回答

1条回答 默认 最新

  • duan7772 2013-03-19 16:35
    关注

    Move your for loop outside of the outer for loop, which is causing it to be executed on every iteration.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器