我想在codeigniter中的foreach循环中显示广告

时间:2016-07-15 10:05:20

标签: php codeigniter

我想要在视图中显示一些项目,我的每页设置为15,因此每个页面显示15个项目,但我想在循环中间显示广告,之后可能显示4个项目,我'我试图在foreach循环中设置if语句来控制显示,它显示前3个值,但是当我为广告添加div时,它也会循环,有人可以告诉我该怎么做,或者指向我正确的方向?谢谢,到目前为止这是我的代码:

      <?php
        $counter1 = 0; 
    //the foreach loop that retrieves the values from the controller    
    foreach($records as $record){

//an if statement to display the first 4 items..
                 if ($counter1 <= 3){ 
                                ?>

                                <div class='box-scene'>
                                   <div class='dbox'>
                                      <div class='front face'>
                                         <a href="#"><img src="images/newtag.png"></a>
                                      </div>
                                      <a style="font-size:15px;" href="<?php echo base_url();?>music/<?php echo $record->url; ?>">
                                      <div class="side face">
                                         <span>
                                            <?php echo $record->name; ?>
                                         </span>
                                      </div>
                                      </a>
                                   </div>
                                </div>


                            <?php 
                              $counter1++;
                            } 
                            ?>



                 <div style="width:200px; height:200px; float:left; display:inline-block; margin: 0 12.5px 20px 12.5px;">
               <div id="ad_200_200">  
               </div>
            </div>
 <!-- this div displays more than once, i dont know where to place it
  for it to display after the first 3 items -->


                    <?php
                    } 
                    ?>

我想知道在哪里放置div,以及如何继续显示这些项目...谢谢

2 个答案:

答案 0 :(得分:0)

还使用如下所示的else语句:

<?php
  $counter1 = 0; 
  foreach($records as $record){
    if ($counter1 <= 3){ ?>
              <div class='box-scene'>
                <div class='dbox'>
                   <div class='front face'>
                      <a href="#"><img src="images/newtag.png"></a>
                    </div>
                    <a style="font-size:15px;" href="<?php echo base_url();?>music/<?php echo $record->url; ?>">
                       <div class="side face">
                          <span>
                              <?php echo $record->name; ?>
                           </span>
                       </div>
                      </a>
                    </div>
                 <div>
                  <?php 
                    $counter1++;
                  } else{ ?>
                      <div style="width:200px;height:200px;float:left;display:inline-block; margin: 0 12.5px 20px 12.5px;">
                          <div id="ad_200_200"></div>
                      </div>
                    <?php 
                     $counter1 = 0;
                    } ?>
                <?php
                } 
                ?>

答案 1 :(得分:0)

您好请使用else condition.please check替换为以下代码

<?php
$counter1 = 0;

//the foreach loop that retrieves the values from the controller    
foreach ($records as $record) {
//an if statement to display the first 4 items..
        ?>
        <?php if($counter1 % 4 == 0) { ?>
            <div style="width:200px; height:200px; float:left; display:inline-block; margin: 0 12.5px 20px 12.5px;">
                <div id="ad_200_200">  
                </div>
            </div>
        <?php } ?>

        <div class='box-scene'>
            <div class='dbox'>
                <div class='front face'>
                    <a href="#"><img src="images/newtag.png"></a>
                </div>
                <a style="font-size:15px;" href="<?php echo base_url(); ?>music/<?php echo $record->url; ?>">
                    <div class="side face">
                        <span>
                            <?php echo $record->name; ?>
                        </span>
                    </div>
                </a>
            </div>
        </div>

    <!-- this div displays more than once, i dont know where to place it
     for it to display after the first 3 items -->

    <?php
    $counter1++;
}
?>