Leave empty cells in an HTML table

1

How can I leave empty cells in a table if I do not want anything to appear in that field? I am sending to call names of some subjects registered in my database, I send them to call and bring them to me there is no problem, the problem is when in a cell if it does not fulfill the condition that I am telling you to leave the field in white and put in the corresponding field, EXAMPLE:

In the Monday field it is supposed to be blank, since there is no class in that schedule, and the Wednesday field also has to be blank, but they do not fit in.

I leave here the code:

<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? Thanks

Fields in my time table:

Send me this error:

    
asked by Emanuel Mejia 04.08.2018 в 08:43
source

3 answers

1

The problem is basically that you need the days to locate each subject.

This can be done in many ways, one of which I propose is to use the days to limit the number of items printed per row.

Next, compare the corresponding day with the existing day in the array that returns the query.

  

I created it with simulated data.

It would stay like this:

<?php
$dias=[
    'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes',
];
//$query=[
//    ['materia'=>'Arquitectura de computadoras', 'dia' => 'Martes'],
//    ['materia'=>'Modelos de Pronosticos', 'dia' => 'Jueves'],
//    ['materia'=>'Modelos de Pronosticos', 'dia' => 'Viernes'],
//];
$query  = $conexion->query('SELECT materia, dia FROM horario WHERE hora = 8');
$result = $query->fetch_all(MYSQLI_ASSOC);
?>
<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($dias as $dia) {
            $key = array_search($dia, array_column($result, 'dia'));
            if(false !== $key) {
                echo "<td>{$result[$key]['materia']}</td>";
            }
            else {
                echo "<td>Disponible</td>";
            }
        } ?>
    </tr>
    <tr>
        <td>9:00 - 10:00</td>
    </tr>
    <!-- /... -->
</table>

This is the exit:

<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>
        <td>Disponible</td>
        <td>Arquitectura de computadoras</td>
        <td>Disponible</td>
        <td>Modelos de Pronosticos</td>
        <td>Modelos de Pronosticos</td>            
    </tr>
    <tr>
        <td>9:00 - 10:00</td>
    </tr>
    <!-- /... -->
</table>
    
answered by 04.08.2018 / 10:19
source
1

I have done something similar before, the ideal would be to create a function to get the day and time as required but while testing this to see if it is what you are looking for.

<tr>
    <td>8:00 - 9:00</td>
    <?php
    //Obtienes el resultado del Lunes a las 8
    $dato = $conexion->query("SELECT * FROM horario WHERE hora = 8 AND dia = 'Lunes'");
    if($dato!=null){ ?>
    <td><?php echo $dato['materia']; ?></td>
    <?php } 
    else{ ?>
    <td>Disponible</td>
    <?php } ?>
    <?php
    //Obtienes el resultado del Martes a las 8
    $dato = $conexion->query("SELECT * FROM horario WHERE hora = 8 AND dia = 'Martes'");
    if($dato!=null){ ?>
    <td><?php echo $dato['materia']; ?></td>
    <?php } 
    else{ ?>
    <td>Disponible</td>
    <?php } ?>
</tr>

I hope to let you understand but what you have to do is: in each "tr" hour you must go every day until Friday. According to the cell in which you find yourself, perform a query query. This code can be optimized for example by creating a method that sends the time and day you return the value, but the central idea is expressed pretty well I think.

    
answered by 04.08.2018 в 10:18
-1

What I'm seeing is that you do not restart the variable "$ data" with each loop and that's why, maybe that happens to you. What I would do would be the following:

<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'];
            } 
            $dato = NULL; //Establece valor NULO a la variable?>
            </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>

You can also use the "unset ()" command (eg "unset ($ data);") of PHP to destroy the variable and recreate it later.

I hope it helps you.

    
answered by 04.08.2018 в 09:40