在php中生成这两个带有类(text-left,text-right)序列的div

时间:2018-04-06 15:54:37

标签: php

我如何生成,这两个div与类(text-left,text-right)序列

<div class="blog-post text-left">
    <figure class="post-image">
        <img src="images/image.png" alt="Image">
    </figure>
</div>
<div class="blog-post text-right">
    <figure class="post-image">
        <img src="images/image.png" alt="Image">
    </figure>
</div>
<div class="blog-post text-left">
    <figure class="post-image">
        <img src="images/image.png" alt="Image">
    </figure>
</div>
<div class="blog-post text-right">
    <figure class="post-image">
        <img src="images/image.png" alt="Image">
    </figure>
</div>

等...

1 个答案:

答案 0 :(得分:0)

我写了这个解决方案:

    <?php
    $count = 0;
    $classes = array('text-left', 'text-right');

    for ($i = 0; $i < 10; $i++) {
        $count = ++$count % count($classes);
        $class = $classes[$count];
    ?>
        <div class="blog-post <?php echo $class?>">
            <figure class="post-image">
                <img src="images/image.png" alt="Image">
            </figure>
        </div>
   <?php } ?>