Bootstrap creating a new column for the second title of the same month?

-5

This script has an object that returns records from different months. In the first month there are two records and PHP is creating a new column for the second record of that month.

Can anyone help me?

<?php

class Evento
{
    public $id;
    public $data;
    public $titulo;

    public function __construct($id, $data, $titulo)
    {
        $this->id     = $id;
        $this->data   = $data;
        $this->titulo = $titulo;
    }
}

$eventos = [
    new Evento(1, new \DateTime('2015-01-26'), 'Titulo #1'),
    new Evento(1, new \DateTime('2015-01-31'), 'Titulo #2'),
    new Evento(1, new \DateTime('2015-03-02'), 'Titulo #3'),
    new Evento(1, new \DateTime('2015-05-04'), 'Titulo #4'),
    new Evento(1, new \DateTime('2015-05-08'), 'Titulo #5'),
    new Evento(1, new \DateTime('2015-08-01'), 'Titulo #6'),
    new Evento(1, new \DateTime('2015-09-14'), 'Titulo #7'),
    new Evento(1, new \DateTime('2015-09-19'), 'Titulo #8'),
    new Evento(1, new \DateTime('2015-11-10'), 'Titulo #9')
];

?>

<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <style>
        .col-md-3 {
            border: 1px solid #f4f4f4;
            padding: 25px 15;
            height: 200px;
            margin-bottom: 25px;
        }
    </style>
</head>
<body>
<?php

echo '<div class="container"><div class="row">' . PHP_EOL;

$mesAnterior = null;

foreach ($eventos as $evento) {

    $mesAtual = $evento->data->format('F');

    echo '<div class="col-md-3">' . PHP_EOL;

    if ($mesAtual != $mesAnterior) {

        echo $mesAtual . '<br>' . PHP_EOL;       

    }

    echo $evento->titulo . '<br>' . PHP_EOL;
    $mesAnterior = $mesAtual;

    echo '</div>';

}

echo '</div></div>' . PHP_EOL;

?>
</body>
</html>
    
asked by anonymous 21.09.2015 / 16:06

1 answer

4

The problem is in the logic of your foreach and not in Bootstrap

Change foreach to this that will work:

foreach ($eventos as $evento) {

    $mesAtual = $evento->data->format('F');

    if (!is_null($mesAnterior) && $mesAtual != $mesAnterior){
        echo '</div>';
    }

    if ($mesAtual != $mesAnterior){
        echo '<div class="col-md-3">' . PHP_EOL;
        echo $mesAtual . '<br>' . PHP_EOL;       
    }

    echo $evento->titulo . '<br>' . PHP_EOL;

    $mesAnterior = $mesAtual;

}

In your current code, it is always opening and closing a new div

    
21.09.2015 / 16:17