ProcessWire foreach循环在每行中显示4列

时间:2015-01-30 07:17:09

标签: php html css processwire

我已经在网站上工作了很长一段时间,偶然发现了创建特定foreach循环的问题。我正在使用ProcessWire为网站创建CMS。我想在每行显示4列,但我感到困惑。这是我到目前为止的代码;

<div class="container">
<? $count == 0; ?>      
        <? foreach($page->events_tickets as $cols): ?>
        <!-- portfolio item -->
        <? if ($count % 4 == 0)
            echo "<div class='row'>";
        ?>
            <? for($count = 0; $count < 4; $count++) { ?>
            <div class="span3  project-item graphics box">
                <div class="thumbnail" >
                    <!-- IMAGE CONTAINER-->
                    <a rel="prettyPhoto[gallery]" href="<?=$cols->event_url;?>" title="portfolio image">
                        <img src="<?=$cols->event_img->url;?>" alt="<?=$cols->event_img->description;?>" /> 
                    </a>
                    <!--END IMAGE CONTAINER-->
                    <!-- CAPTION --> 
                    <div class="caption">
                        <h4 class=""><?=$cols->event_title;?></h4>
                            <p class="caption-descr" id="opening-reception" style="height: 190px; overflow:auto;">
                            <?=$cols->event_desc;?> <a href="<?=$cols->event_url;?>" target="_blank" style="" title=""> BUY TICKETS</a> 
                            </p>    
                    </div><!--END CAPTION --> 
                </div><!-- END: THUMBNAIL -->
            </div><!-- END: portfolio item -->
            <? if($count % 1 = 1 ) break;} ?>
            </div>  <!--END OF ROW-->
        <? endforeach; ?>

1 个答案:

答案 0 :(得分:3)

没关系,我终于找到了如何解决它。而是使用嵌套循环,我决定通过条件if else语句运行循环。

<div class="row">
            <? foreach($page->events_tickets as $cols): ?>
            <? if($cols->event_num % 4 != 0) {?>
            <!-- portfolio item -->
                <div class="span3  project-item graphics box">
                    <div class="thumbnail" >
                        <!-- IMAGE CONTAINER-->
                        <a rel="prettyPhoto[gallery]" href="<?=$cols->event_url;?>" title="portfolio image">
                            <img src="<?=$cols->event_img->url;?>" alt="<?=$cols->event_img->description;?>" /> 
                        </a>
                        <!--END IMAGE CONTAINER-->
                        <!-- CAPTION --> 
                        <div class="caption">
                            <h4 class=""><?=$cols->event_title;?></h4>
                                <p class="caption-descr" id="opening-reception" style="height: 190px; overflow:auto;">
                                <?=$cols->event_desc;?> <a href="<?=$cols->event_url;?>" target="_blank" style="" title=""> BUY TICKETS</a> 
                                </p>    
                        </div><!--END CAPTION --> 
                    </div><!-- END: THUMBNAIL -->
                </div><!-- END: portfolio item -->

            <? } else if($cols->event_num%4 == 0) {?>
            <div class="row">
                <div class="span3  project-item graphics box">
                        <div class="thumbnail" >
                            <!-- IMAGE CONTAINER-->
                            <a rel="prettyPhoto[gallery]" href="<?=$cols->event_url;?>" title="portfolio image">
                                <img src="<?=$cols->event_img->url;?>" alt="<?=$cols->event_img->description;?>" /> 
                            </a>
                            <!--END IMAGE CONTAINER-->
                            <!-- CAPTION --> 
                            <div class="caption">
                                <h4 class=""><?=$cols->event_title;?></h4>
                                    <p class="caption-descr" id="opening-reception" style="height: 190px; overflow:auto;">
                                    <?=$cols->event_desc;?> <a href="<?=$cols->event_url;?>" target="_blank" style="" title=""> BUY TICKETS</a> 
                                    </p>    
                            </div><!--END CAPTION --> 
                        </div><!-- END: THUMBNAIL -->
                    </div><!-- END: portfolio item -->
                    </div>  <!--END OF ROW-->
                <? } ?> 
            <? endforeach; ?>
        </div>  <!--END OF ROW-->

我在events_tickets字段中添加了额外字段,并将其称为events_numevents_num以1开头,一直跟踪到记录结束。<? if($cols->event_num % 4 != 0) {?>然后显示4列。然后,如果数字达到5,除以4时的余数是奇数,因此它创建<div class="row">并再显示4列。然后这个过程继续。因此,不需要创建嵌套循环。