I have a FOR in PHP and I need a row to show me option A and the other row option B

2

I have a question. Currently I have a timeline in html which shows me one on the right and the other on the left, but I want to integrate it with dynamic data.

To put right start only with <li> But to put it to the left I start it with <li class="time-inverted">

What I want is the first one I put it on the left and the next one on the right and so on, depending on the amount I bring.

This is my php query:

public function historial() {
    $id_activo = $this->input->post('id_activo');
    $historial = $this->stock->historial($id_activo);
    $resp = '';
    foreach ($historial as $row) {

        $resp .= "<ul class='timeline'>
                            <li>
                                <div class='timeline-panel'>
                                    <div class='timeline-heading'>
                                        <h4 class='timeline-title'>" . $row ['nombre_completo'] . "</h4>
                                    </div>
                                    <p><small class='text-muted'><i class='glyphicon glyphicon-time'></i> " . $row ['fecha_asignada'] . "</small></p>
                                    <div class='timeline-body'>
                                        <p>" . $row ['tipo'] . " - " . $row ['descripcion'] . "</div>
                                </div>
                            </li>
                        </ul>";
    }
    echo json_encode($resp);
}

As you can see here I only use in <li> but the query returns 2 data or in other cases more data, I do not know how to put the second parameter in <li class="time-inverted"> and the one that follows it to the right and in that way

    
asked by Ivan More Flores 24.10.2018 в 18:52
source

1 answer

2

Declare a boolean variable in the following way:

$izquierda = true;

Finally the code:

public function historial() {
    $id_activo = $this->input->post('id_activo');
    $historial = $this->stock->historial($id_activo);
    $resp = '';
    $izquierda = true;
    foreach ($historial as $row) {

        $resp .= "<ul class='timeline'>";
        if ($izquierda) {
            $resp .= "<li>";
        } else {
            $resp .= "<li class='timeline-inverted'>";
        }
        $izquierda = !$izquierda;
        $resp .= " <div class='timeline-panel'>
                                    <div class='timeline-heading'>
                                        <h4 class='timeline-title'>" . $row ['nombre_completo'] . "</h4>
                                    </div>
                                    <p><small class='text-muted'><i class='glyphicon glyphicon-time'></i> " . $row ['fecha_asignada'] . "</small></p>
                                    <div class='timeline-body'>
                                        <p>" . $row ['tipo'] . " - " . $row ['descripcion'] . "</div>
                                </div>
                            </li>
                        </ul>";
    }
    echo json_encode($resp);
}

In each iteration, we change the value of $ left, in such a way to know which side is printed

    
answered by 24.10.2018 / 19:16
source