PHP/Bootstrap - Replay up to X and then up to Y then includes new DIV and continues replay

Asked

Viewed 97 times

1

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

Currently I have this block of codes 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 that every 6 records returned from the bank, it add a new div Row below and continue reading until it totals 4 Rows, because then I will see how to make paging.

I located a code here in Overflow, very similar to 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)){?>

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

Thanks in advance.

  • What have you ever tried to do that your goal? Just one thing, take the { of the end of the foreach since you’re using the :. Now come on, if I get it, every six posts returned, you want to do the line break, is that it? You will need to put two counters there, one for number of posts per line (6) and one for number of lines (4).

  • Oops! the idea is that every 6 post he adds a new div Row. I found a code that fits what I want, I tried to embed it in my reality, it jumped right line, after my adaptations, see:

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.