How can I leave empty cells in a table if I don't want anything to appear in that field? I am sending for some names of some subjects registered in my database, I send for them and they bring them there, there is no problem, the problem is when in a cell if it does not meet the condition that I am telling it to leave the field in blank and put in the corresponding field, EXAMPLE:
In the Monday field it is supposed to be blank, since there is no class at that time, and the Wednesday field also has to be blank, but they do not accommodate
I leave the code here:
<body>
<?php require 'views/navbar.php'; ?>
<div class="contenedor">
<h2>Horario de clases</h2>
<table>
<thead>
<tr>
<th>HORA</th>
<th>LUNES</th>
<th>MARTES</th>
<th>MIERCOLES</th>
<th>JUEVES</th>
<th>VIERNES</th>
</tr>
</thead>
<tr>
<td>8:00 - 9:00</td>
<?php foreach($datos = $conexion->query('SELECT * FROM horario WHERE hora = 8') as $dato){ ?>
<td><?php if(empty($dato)){
echo "Disponible";
}else{
echo $dato ['materia'];
} ?>
</td>
<?php } ?>
</tr>
<tr>
<td>9:00 - 10:00</td>
</tr>
<tr>
<td>10:00 - 11:00</td>
</tr>
<tr>
<td>11:00 - 12:00</td>
</tr>
<tr>
<td>12:00 - 13:00</td>
</tr>
</table>
</div>
Any idea how I can do it? Thank you
Fields of my time table:
It sends me this error:
I've done something similar before, ideally you'd create a function to get the day and time as required but in the meantime try this and see if it's what you're looking for.
I hope I understand but what you have to do is: in each "tr" of hour you must go through every day until Friday. Depending on the cell you are in, make a query of queries. This code can be optimized, for example, by creating a method that sends the time and day and returns the value, but the central idea is expressed quite well, it seems to me.
The problem basically is that you need the days to locate each matter.
This can be done in many ways, one of the ways I propose is to use days to limit the number of items that are printed per row.
Then, compare the corresponding day with the existing day in the array returned by the query.
It would look like this:
This is the output:
What I'm seeing is that you don't reset the "$data" variable with each loop and that's why, maybe, that happens to you. What I would do is the following:
You can also use PHP's "unset()" command (eg "unset($data);") to destroy the variable and have it recreated later.
I hope it helps.