Difficulties getting values inside an array

0

I am making a code where I select information from the database and feed an array, in the sequence I am trying to use the contents of this array to compare with other information and process the rest of the code.

But I'm having trouble getting the data from within the array, below my code:

<?php
    $fuso    = mktime(date("H")-3, date("i"), 0);
    $hoje    = gmdate("Y-m-d", $fuso);
    //$resultado_array = array(); //sem o array
    $agendamentos = $dbh->query("SELECT * FROM agenda WHERE data_agendamento='$hoje'");
    while($row = $agendamentos->fetch(PDO::FETCH_ASSOC)) {

        if ($row['hora_agendamento'] == "08:00:00") { //agora com $row
            echo '<tr>';
            echo '<td>' . $row['hora_agendamento'] . '</td>';
            echo '<td>' . $row['id_paciente'] . '</td>';
            echo '<td>' . $row['observacao'] . '</td>';
            echo '<td>' . $row['id_agendamento'] . '</td>';
            echo '<td>Editar</td>';
            echo '</tr>';
        }
        else {
            echo '<tr>';
            echo '<td>08:00:00</td>';
            echo '<td></td>';
            echo '<td></td>';
            echo '<td></td>';
            echo '<td>Editar</td>';
            echo '</tr>';
        }
        if ($row['hora_agendamento'] == "09:00:00") { //agora com $row
            echo '<tr>';
            echo '<td>' . $row['hora_agendamento'] . '</td>';
            echo '<td>' . $row['id_paciente'] . '</td>';
            echo '<td>' . $row['observacao'] . '</td>';
            echo '<td>' . $row['id_agendamento'] . '</td>';
            echo '<td>Editar</td>';
            echo '</tr>';
        }
        else {
            echo '<tr>';
            echo '<td>09:00:00</td>';
            echo '<td></td>';
            echo '<td></td>';
            echo '<td></td>';
            echo '<td>Editar</td>';
            echo '</tr>';
        }

    } //while agora só termina aqui

    echo '</tbody></table>';
?>

In case if I find inside the array the string there determined it should fill in the data below, but at the beginning if it already presents the error:

  

Notice: Undefined index: schedule_time in   C: \ wamp64 \ www \ schedules \ index.php on line 29

Clearly because I can not get the data inside the array, how do I proceed?

    
asked by anonymous 06.10.2017 / 02:52

1 answer

1

What you are trying to do does not make much sense. You are saving all the rows that come from the table in an array:

while($row = $agendamentos->fetch(PDO::FETCH_ASSOC)) {
    array_push($resultado_array,$row); //<- aqui com array_push

But then use it as if it were just a normal object and not an array:

if ($resultado_array['hora_agendamento'] == "08:00:00.00000") {

To use as an array you have to specify the position, for example:

if ($resultado_array[0]['hora_agendamento'] == "08:00:00.00000") {

Notice that I indicated position 0 with [0] .

But this creates another problem because either you just use the first one (and it may not even exist!) or you must use another while to use the various lines. It's even better to use the while you already had initially and apply the logic and writing there:

<!--O inicio da tabela é html normal por isso pode ficar escrito como html-->
<table id="tableteste" class="table table-striped" width="100%">
    <thead>
        <tr>
            <th>Hora</th>
            <th>Paciente</th>
            <th>Observação</th>
            <th>Agendamento</th>
            <th>Editar</th>
        </tr>
    </thead>
<tbody>

<?php
    $fuso    = mktime(date("H")-3, date("i"), 0);
    $hoje    = gmdate("Y-m-d", $fuso);
    //$resultado_array = array(); //sem o array
    $agendamentos = $dbh->query("SELECT * FROM agendamentos WHERE data_agendamento='$hoje'");

    while($row = $agendamentos->fetch(PDO::FETCH_ASSOC)) {

        if ($row['hora_agendamento'] == "08:00:00.00000") { //agora com $row
            echo '<tr>';
            echo '<td>' . $resultado_array['hora_agendamento'] . '</td>';
            echo '<td>' . $resultado_array['id_paciente'] . '</td>';
            echo '<td>' . $resultado_array['observacao'] . '</td>';
            echo '<td>' . $resultado_array['id_agendamento'] . '</td>';
            echo '<td>Editar</td>';
            echo '</tr>';
        }
    } //while agora só termina aqui

    echo '</tbody></table>';
?>
    
06.10.2017 / 12:53