Problem when receiving data from the database with if/Else

Asked

Viewed 49 times

-1

Guys I have a problem in my php, I want to select the name of people according to a code and for this I made an IF, but the code that should take the values of the database and print on the page is repeating a command before printing the next value. I appreciate anyone who can help me.

Code:

PHP - Query:

<?php include("config.php");
    $consulta1 = $MySQLi -> query("SELECT * FROM TB_HOSPITAIS");
    $consulta2 = $MySQLi -> query("SELECT * FROM TB_MEDICOS 
      join TB_PLANTOES on MED_PLANT_CODIGO = PLANT_CODIGO
      join TB_HORARIOS on PLANT_HOR_CODIGO = HOR_CODIGO");
?>

Table - Showing the values:

<table class="table table-striped" border="2" align="center text-center">
              <tr>
                <th scope="col">#</th>
                <th scope="col">Medico(a)</th>
                <th scope="col">Horário</th>
              </tr>
              <?php while($resultado2 = $consulta2 -> fetch_assoc()) {?>
                <tr>
                  <th scope="row">Domingo</th>
                  <td> 
                    <?php if ($resultado2['MED_PLANT_CODIGO'] == 1)
                      echo $resultado2['MED_NOME']; ?> 
                  </td>
                  <td> <?php echo $resultado2['HOR_HORARIO']; ?> </td>
                </tr>
                <tr>
                  <th scope="row">Segunda</th>
                  <td> 
                    <?php if ($resultado2['MED_PLANT_CODIGO'] == 2)
                      echo $resultado2['MED_NOME']; ?> 
                  </td>
                  <td> <?php echo $resultado2['HOR_HORARIO']; ?> </td>
                </tr>
              <?php } ?>          
            </table>

Config.php:

<?php
    session_start();
    $endereco = "localhost";
    $usuario = "root";
    $senha = "";
    $banco = "DB_SCH";

    $MySQLi = new mysqli($endereco, $usuario, $senha, $banco, 3306);
    if(mysqli_connect_errno()){
        die(mysqli_connect_error());
        exit();
    }
    mysqli_set_charset($MySQLi,"utf8");

    function br_us($data) {
        $data = implode("-",array_reverse(explode("/",$data)));
        return $data;
    }
    function us_br($data) {
        $data = implode("/",array_reverse(explode("-",$data)));
        return $data;
    }
?>

In the table instead of printing the name of the doctor on duty 1 on Sunday and then the name of the doctor on duty 2 on Monday, he first prints Monday as empty, generates a new table and prints the value of Monday and leaves Sunday empty.

2 answers

0


There seem to be two problems in your logic

  1. You expect the data to be ordered starting on Sunday, but you don’t order it
  2. Every loop prints every day

Change your query to

$consulta2 = $MySQLi -> query("SELECT * FROM TB_MEDICOS 
  join TB_PLANTOES on MED_PLANT_CODIGO = PLANT_CODIGO
  join TB_HORARIOS on PLANT_HOR_CODIGO = HOR_CODIGO
  ORDER BY MED_PLANT_CODIGO ASC");

And your loop logic for

<?php while($resultado2 = $consulta2 -> fetch_assoc()) {?>
<tr>
  <th scope="row"><IMPRIMIR DIA></th>
  <td><?php echo $resultado2['MED_NOME']; ?></td>
  <td><?php echo $resultado2['HOR_HORARIO']; ?></td>
</tr>
<?php } ?>

Instead of having a logic of IFThe way you’re doing, it would be better to have something like

$dias = array("1" => "Domingo", "2" => "Segunda");

And in your loop would

<?php echo $dias[$resultado2['MED_PLANT_CODIGO']]; ?>

Putting the pieces together:

<?php
$dias = array("1" => "Domingo","1" => "Segunda");
while($resultado2 = $consulta2 -> fetch_assoc()):
?>
<tr>
  <th scope="row"><?php echo $dias[$resultado2['MED_PLANT_CODIGO']]; ?></th>
  <td><?php echo $resultado2['MED_NOME']; ?></td>
  <td><?php echo $resultado2['HOR_HORARIO']; ?></td>
</tr>
<?php endif; ?>

PS: This logic solves the problems presented. However, there may be other problems, such as the query returning more than one result per day. In this case they would be listed every Sunday, followed by Mondays, ...

  • Could you explain to me how to leave the days of the week fixed in the <td> and then the code that corresponded to that day, would appear there? Because my idea was Friday, Saturday and Sunday to receive 2 names in the same <td> and is being created a new <td> for the second name.

  • @I couldn’t quite understand what you want to do, but it seems to me it would be a new question. it would also be important to add examples of database records, which is expected to be returned by query and what/how should be presented.

-3

@tvdias, really happened as you said, he listed every Sunday and last every Monday, for that I put an appointment by . See:

<?php while($resultado2 = $consulta2 -> fetch_assoc()) {?>
                <tr>
                  <th scope="row"><?php echo $dias[$resultado2['MED_PLANT_CODIGO']]; ?></th>
                  <td><?php echo $resultado2['MED_NOME']; ?></td>
                  <td><?php echo $resultado2['HOR_HORARIO']; ?></td>
                </tr>
              <?php } ?>
              <?php while($resultado3 = $consulta2 -> fetch_assoc()) {?>
                <tr>
                  <th scope="row"><?php echo $dias[$resultado3['MED_PLANT_CODIGO']]; ?></th>
                    <td><?php echo $resultado3['MED_NOME']; ?></td>
                    <td><?php echo $resultado3['HOR_HORARIO']; ?></td>
                </tr>
              <?php } ?>  

Browser other questions tagged

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