dplm47571 2016-11-30 22:14
浏览 200
已采纳

在foreach循环中插入随机块

I have posts that I am displaying through a foreach loop. Within this I would like to throw in some advertising blocks.



I have posts and advertising blocks working with no issues. The posts are coming from an array and the advertising blocks I am manually inserted.

The hiccup I am seeing is how I am throwing in these ads into the foreach loop. I want it to add them but not interrupt the posts themselves and that is what I am experiencing.



I have 3 advertising blocks and 12 posts per page. When the page loads there should be a total of 15 blocks and instead I am getting 9. Those ads also not static - I have them coming in to be placed anywhere within those 15 blocks in total.


Here is quick run down of the script I am using (a readers digest version).



$i = 0;
foreach ($posts AS $post) {

   $i++;

   if ($adblock == $i) {
        //insert advertisement block
   }
   else if ($adblock == $i) {
        //insert advertisement block
   }
   else if ($adblock == $i) {
        //insert advertisement block
   }
   else {
        //insert post block
   }

}
  • 写回答

1条回答 默认 最新

  • dongxi1965 2016-11-30 22:22
    关注

    Without too much complexity, if the number of posts/adv per page is static, you can use a simple math to place your adv like this:

    $i=0;
    foreach($posts as $post){
      $i++; 
    
      if($i%3==0) { /* insert additional adv block */ }
    
      /* insert post block at anytime*/
    }
    

    you can change the ($i%3==0) to any other number; with 3 you will get an adv every 3 posts. need more adv, make it ($i%2==0), need less, make it ($i%4==0), etc.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog