Difficulties picking up values within an array

Asked

Viewed 95 times

0

I’m making a code where I select database information and feed an array, then I’m trying to use the contents of that array to compare with other information and process the rest of the code.

But I’m having trouble getting the data from inside 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 you find inside the array the string determined there it should fill the data below, but just when starting the if already presents the error:

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

Clearly because I’m not getting the data inside the array, how to proceed?

  • But the idea was to build a table with all the schedules that have time 08:00 ? Or just the first ?

  • All, but you’ll only have one appointment, I won’t allow more than one appointment at the same time on the same day. In this code it is only a parameter for when find in the array this time, it fill the table with the rest of the data of that row of the array.

  • tried to use var_dump to check the structure of your array?? Try running like this right after while: var_dump($resultado_array); and see q returns, if you still can’t find the path to get your array, edit your question with the result of var_dump.

  • hora_agendamento is not an index value of the array. An array’s index value is a number (0, 1, 2, 3...).

1 answer

1

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

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

But then uses 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 have to use another one again while to use the various lines. It is better to use the while who already had initially and apply the logic and written 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>';
?>
  • My code was exactly like this (I edited the main post there), but as you can see, when there is if and Else, when the result fits into Else the code repeats several times the result, see only when there are no records for the time 09:00:00: https://imgur.com/a/WMRaD

  • @Fernandogross It can’t be that way. It has to be the first if for "08:00:00" afterward else if for "09:00:00" and then the else normal. That would be the way to do if you want to have 2 cases and a third for everything else. But in your code, I couldn’t even tell the difference between the various cases, because they look the same to me except for the time. And the time is already shown in the table, so the if nor does it seem to make sense.

  • My God, I’m retarded haha... The idea of these if Else is to fill the table with the times that have data in the database and those that do not generate blank lines, then when clicking edit I will open a modal or something to insert a new schedule in that unoccupied space, for now it doesn’t make much sense at all, and it was the most practical way that I found to make an agenda that fit my plan, there may be another way, but using what I know apparently is this here.

Browser other questions tagged

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