douliao1911 2015-11-13 14:59
浏览 12
已采纳

php +1每套内容

I'm creating a series of content blocks. (4 in total).

It's stupid to create all for of them if you can let php create them for you. So I created the first one and wrote a for loop that adds the content as long as it haven't a max number of 4.

Here it is:

<table>
<tr>
<?php 
    $counter =0;
    for ($i=0; $i <= 3; $i++){
        $counter++;
        ?>
        <td>some content to repeat with id='$id++' and '$id++'</td>
        <?php
    if ($counter == 2){
        echo '</tr><tr>';
        $counter=0;
    }
}
?>
</tr>
</table>

What this does is repeat the <td> two times and then end the rule and start a new one. Do this until you reach an amount of 4 (3 because 1=0)

The problem I have is that some of the content contains id's
The id's should also number up but only if the complete loop is repeated.
So $i++ should both have the same value if the loop runs once

So that would mean that if there are two id's in the first run they should both have the id1 and the next repeat should have id2

I have no idea how to do this.

The output should be:

<table>
    <tr>
        <td>
           This content has id 1   
           And this content has id 1 to    
        <td>
        <td>
           This content has id 2   
           And this content has id 2 to 
        <td>
    </tr>
    <tr>
        <td>
           This content has id 3   
           And this content has id 3 to 
        <td>
        <td>
           This content has id 4   
           And this content has id 4 to 
        <td>
    </tr>
</table>

M.

  • 写回答

2条回答 默认 最新

  • douzhi7070 2015-11-13 15:23
    关注

    How about this

    <table>
    <?php 
        $id = 1
        for ($i=0; $i < 2; $i++){
            echo '<tr>';
            for ($j=0; $j < 2; $j++){
                echo 'This content has id $id';
                echo' And this content has id $id too';
            }
            $id += 1;
            echo '</tr>';
        }
    }
    ?>
    </table>
    

    Update: added an id counter.

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

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集