douhan4093 2012-05-20 18:49
浏览 70
已采纳

如何在2次运行后暂停循环,在两次之间插入代码,然后继续循环停止?

I currently have a loop of items being shown on a page.

I would like to run the loop twice (output 2 results/items), and THEN insert a banner ONCE right after the 2nd result, and THEN let loop continue from where it left off.

An illustration below:

{--- START OF LOOP ---}

ITEM 1 | ITEM 2
{ BANNER HERE }
ITEM 3 | ITEM 4
ITEM 5 | ITEM 6
ITEM 7 | ITEM 8

{--- END OF LOOP ---}
<- prev | next ->

The items are inside a while loop:

$sql = mysql_query("SELECT id, img, description, keyword, category FROM images WHERE connect = 1 ORDER BY id DESC LIMIT $from , $perPage");

while($result = mysql_fetch_object($sql)) {
LOOP OF ITEMS HERE }

I've only ever found "do" / "while" methods, which isn't really what I'm looking for. If anyone could help, thank you for your time & assistance.

  • 写回答

2条回答 默认 最新

  • dongyan5815 2012-05-20 18:57
    关注
    $i=1;
    while($result = mysql_fetch_object($sql)) {
        //print item
        if($i==2) { 
            //print banner
        }
        $i++;
    }
    

    if you want it to print every 2 items the yu can use

    if($i % 2 == 0)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测