PHP / Bootstrap - Repeat until X and then until Y then adds new DIV and continues the replay

1

I'm breaking my head in this repetition, as this is my first question, sorry for the mistakes. :)

I'm currently with this code block inside the container:

<?php  
 echo '<div class="row">';
            $posts = DBRead('posts', "WHERE status = 1 ORDER BY data DESC");
            if (!$posts)
                echo '<h2>Nenhum post foi encontrado.</h2>';
            else        
                foreach ($posts as $post): {
                    $categ = DBRead('categorias', "WHERE id = '". $post['categoria']."'");
                    $categ = ($categ) ? $categ[0]['titulo'] : 'SEM CATEGORIA |' ;}
            echo '<div class="col-md-2" style="border:1px solid black">';
        ?>      
                <h4>
                    <a href="single.php?id=<?php echo $post['id']?>" title="<?php echo $post['titulo']; ?>">
                        <?php echo $post['titulo']; ?>
                    </a>
                </h4>

                <p>
                    por <b><?php echo $post['autor'] ?></b>
                    em <b><?php echo date('d/m/Y', strtotime($post['data'])) ?></b> |
                    <b><?php echo $categ ?></b> 
                    Visitas <b><?php echo $post['visitas'] ?></b>
                </p>
<?php  
    echo '</div>';
    endforeach;
?>

My goal is for every 6 records returned from the database, it adds a new div row below and continues reading, until it totals 4 rows, then I'll see how to do paging.

I found a code here in OverFlow, much like what I want. I adapted it to my reality, see:

<?php 
$postLine = 6;
$posts = DBRead('posts', "WHERE status = 1 ORDER BY data DESC");
foreach($posts as $post => $post){ //$posts array com dados do mysql
  $categ = DBRead('categorias', "WHERE id = '". $post['categoria']."'");
  $categ = ($categ) ? $categ[0]['titulo'] : 'SEM CATEGORIA |' ;

if(($post%$postLine==0) || ($post==0)){ //gera linha com base no numero de          items
?>
<div class="row">
<?php } ?>

<div id="conteudo" class="col-md-2 span7 text-center"><!-- START Video         Content -->
    <div class="video_block">
        <a href="#" class="thumbnail">
            <img src="" alt="produto"/>
        </a>
        <div class="caption">
            <span class="video_title"><?php echo $post['title'];?></span>
            <br />
        </div>
    </div>
</div>

<?php if(($post%$postLine==0) || ($post==0)){?>

    

It has a but ... the first post is above the others and misaligned. Could you help me?

Thank you in advance.

    
asked by anonymous 07.08.2015 / 15:50

0 answers