dongshou6788 2015-01-30 07:17
浏览 42
已采纳

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

I've been working on the website for quite a while now and stumbled upon a problem in creating specific foreach loop. I am using ProcessWire to create CMS for the website. I want to display 4 columns in each row, but I am baffled. Here is my code so far;

<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条回答 默认 最新

  • doue8385 2015-01-31 15:25
    关注

    Nevermind, I eventually found out how to solve it. Instead using nested loops I decided to run the loop through conditional if else statement.

    <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-->
    

    I added extra field to the events_tickets field and called it events_num. events_num starts with 1 and follows along until the end of the records.<? if($cols->event_num % 4 != 0) {?> then display the 4 columns. Then if number reached 5 the remainder when divided by 4 is odd number, thus it creates <div class="row"> and displays 4 more columns. Then this procedure continues. Thus there is no need in creating nested loop.

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

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染