List 3 items side by side

1

I want to make a list of sequence items only with 3 columns, I created an odd or even parser, to do the listing side by side, in the case 2 columns ...

But now I have to do a listing of 3 items per line ... The image below shows what the idea is, but only column 3 is repeating the items in column 2.

<divclass="row p30-top">
                    <div class="col-md-12">
                        <?php
                            if(have_posts()) : the_post();
                                $i = 0;
                                $content = explode("<li>", get_the_content());
                                foreach($content as $row) : 
                                    if($i % 2 == 0) : 
                        ?>
                                <div class="row">
                                    <div class="col-md-4"><?= $row ?></div>
                        <?php else : ?>
                                    <div class="col-md-4"><?= $row ?></div>
                                    <div class="col-md-4"><?= $row ?></div>
                                </div>
                        <?php
                                    endif;
                                    $i++;
                                endforeach;
                                if($i % 2 != 0) { echo '</div>'; }
                            endif;
                        ?>
                    </div>
                </div>

Exemplo de como estou implementando o código:. (Acima)

    
asked by anonymous 26.04.2017 / 16:59

1 answer

1

You can use the arr_chunk function to break the array into pieces of 3:

arr_chunk(array,quantidade);

So you already create the structure and do not deal with the indexes:

<div class="row p30-top">
<div class="col-md-12">
    <?php
        if(have_posts()) : the_post();
        $content = explode("<li>", get_the_content());
        $content = array_chunk($content,3);

        foreach($content as $row){
            echo '<div class="row">';
            foreach($row as $col){
                echo  '<div class="col-md-4">' . $col . '</div>';
            }
            echo '</div>';
        }
    ?>
</div>

This should resolve.

    
27.04.2017 / 16:11