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

Asked

Viewed 60 times

-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 the same month.

Can someone 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>
  • Yes! You can do a counter before foreach() $i = 0, and within the same if != mesAnterior .. $i++; and does a check if($i==0) echo "name of the month".. because there in the next array it already jumps again

  • Well, I asked a new question to solve another part of the question, explained what I needed, developed the code (did not copy), put the example code and I am being NEGATIVE. Can someone explain to me what’s missing or what’s left?

1 answer

4


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

Change the foreach by this which 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 his current code, he’s always opening and closing a new div

Browser other questions tagged

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