2014-03-07 15 views
1

我在Yii實施我的項目,我打電話給Google廣告。它正在顯示,但應根據條件顯示。根據條件動態發佈多條Google廣告

我從數據庫動態調用數據,我打電話給圖像的內容。在顯示一個廣告之後,將顯示10個類別食譜,但是僅在該廣告顯示爲空白之後才顯示一次。

我在這裏寫了我的代碼,請建議我如何修復此代碼。我希望每顯示一個Google廣告的十個類別。

<?php $counter=0; $count123=0; ($posts as $receipe):?> <div class="post"> 
<?php $counter=$counter+1; 
      if($count123<4 && $counter==9) 
      { ?> 

      <div class="col-lg-3 col-md-3 col-sm-3 col-xs-11 wrdLatest" id="imgcontent_rand_recipe"> 
       <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> 

      <ins class="adsbygoogle" id="kraftmonsterresponsive" 
       style="display:block" 
       data-ad-client="xxxxxxxxxxx" 
       data-ad-slot="xxxxxxxxxxxxxxxxxxx" 
       data-ad-format="auto"></ins> 
       <script> 
      (adsbygoogle = window.adsbygoogle || []).push({}); 
     </script> 
     <?php 
      $counter=1; 
      $count123=$count123+1; ?> </div> 
      <?php } ?> 

回答

0

嘗試下面的內容。要點:

  • 我用Modulus Operator - %制定出當你在每第10個項目。
  • 假設您的$posts數組索引0和連續的,我已經去掉了所有的$counter瓦爾 - 你只需要在$k foreach循環
  • 你有缺少一個右</div>標籤,我在
添加

希望對您有所幫助:

// $k is 0 indexed 
foreach($posts as $k => $receipe) 
{ 
?> 
<div class="post"> 
<?php 

    // Print your Post or recipe info here 
    print_r($receipe); 

    // Now check for each 10th item 
    if((($k + 1) % 10) == 0) 
    { 
?> 
    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-11 wrdLatest"> 
     <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> 
     <ins class="adsbygoogle" id="kraftmonsterresponsive" style="display:block" data-ad-client="ca-pub-1823432637478832" data-ad-slot="3981469793" data-ad-format="auto"></ins> 
     <script> 
      (adsbygoogle = window.adsbygoogle || []).push({}); 
     </script> 
    </div> 
<?php 
    } 
?> 
</div><!-- .post --> 

<?php 
}