2
Hello!
My code is a schedule and works as follows, there is an array with some times inside:
$horarios = ["08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00"];
I then make a SELECT in the database looking for schedules and store the data inside an array through a foreach:
$stmt = $dbh->query("SELECT *, date_format(hora_agendamento, '%H%:%i') as hora_agendamento FROM agendamentos WHERE data_agendamento='$data_agendamento_convert' AND medico='$medico_completo'");
$result = $stmt->fetchAll();
After that I walk through an array and check if each time of the $hours array can be found within the $result array:
for ($i=0; $i <= (count($horarios) - 1); $i++) {
if (in_array($horarios[$i], $result[$i])) { ?>
<tr>
<td><?php echo $horarios[$i]; ?></td>
<td><?php echo $result[$i]['nome']; ?></td>
<td><?php echo $result[$i]['descricao']; ?></td>
<td>Editar</td>
</tr>
<?php } else { ?>
<tr>
<td><?php echo $horarios[$i]; ?></td>
<td></td>
<td></td>
<td>Editar</td>
</tr>
<?php } }?>
The first line found is printed correctly, then I believe that because it has 10 times in $hours and only 3 times in $result the code starts to return me the error below:
Notice: Undefined offset: 3 in C: wamp64 www admin agenda1.php on line 91
I imagine that if I improve this my if I can solve the problem, but I’m not sure what to use to improve it, someone can give me a light?
Thank you!
If code can contain problems even in the elaboration, why, fetchAll() does not already return an array ? to do a for? which is line 96?
– novic
Yes, it does, but I need to use this data outside of foreach, so I fed another array with the data that SELECT returns. Line 96: if (in_array($horarios[$i], $agendamentos[$i])) { ?>
– Fernando Gross
No need to generate the data if you have the data!? understands this. the variable result is the same thing as the variable schedules
– novic
Wow, correct, my haha fails, but the Undefined offset problem persists, because the content of the two arrays (scheduling and result) are the same.
– Fernando Gross
Here’s the next problem:
if (in_array($horarios[$i], $result[$i])) { ?>
because theresult[$i]
you are accessing the log and not the array you need, if you need to compare schedules you have to have another strategy. What is the purpose of this part?– novic
In this part I want to know if there are schedules for the schedules contained in the $horarios array, so I made a condition for when I find an equal record he show me in the table the data of that schedule, patient name and schedule description, If you don’t find anything it executes Else and completes the table with empty data.
– Fernando Gross
Good come on! why know the result if SQL brings what has? not just print
$result
? Why compare if you need to show ? think a little! tip takes this check and has print on screen!– novic
This way he prints the lines right one after the other, but now I need him to check the schedule and fill in the correct line, so there was that if, for when you match between the time of $hours and the time of the $result it fill that line with the data, when not this match, ran the Else and filled the blank line, so I had view of the agenda with all the times used and available too, this is the idea.
– Fernando Gross
So do it like this:
if (in_array($result[$i]['hora_agendamento'], $horarios)) { ?>
that is, just invert the variables within the functionin_array
– novic
You are taking the data, just keep giving Undefined offset 3, because it has few results, it goes from 0 to 2 only(3 records found in the database), I need to stop iterating $result because at some point I happen to be wanting data from a non-existent offset inside the array and generates the error, I need a better if.
– Fernando Gross