Values do not stay in the right column in the table

0

<?php$listar=newFuncionarioDAO();$resultTudo=$listar->listar();$results=newFuncionarioDAO();$results=$results->retornaEmpresa();while($row=$resultTudo->fetch_assoc()){?><tr><td><?=$row['id']?></td><?phpforeach($resultsas$values){?><td><?phpif($values['id']==$row['id_empresa']){echo$values['nome'];break;}?></td><?php}?><td><?=$row['nome']?></td><td><?=$row['sobrenome']?></td><td><?=$row['telefone']?></td><td><aclass="label label-warning" href="<?= "funcionario-atualizar?x=".$row['id'];?>">editar</a></td>
                <td><a href="<?= "funcionario-remover?x=".$row['id'];?>" class="label label-danger">remover</a></td>
            </tr>
        <?php }if($resultTudo->num_rows == 0){
            print("<h3 class='btn btn-warning'>Nenhum produto cadastrado ainda</h1>");
        }?>
    
asked by anonymous 03.12.2014 / 15:56

1 answer

4

A patch in your code is this:

        <tr>
            <td><?= $row['id']?></td>
            <td>
            <?php
               foreach($results as $values) {
                  if($values['id'] == $row['id_empresa']) {
                     echo $values['nome'];
                     break;
                  }
               }
            ?>
            </td>
            <td><?= $row['nome']?></td>
            <td><?= $row['sobrenome']?></td>
            <td><?= $row['telefone']?></td>

But that's just patch, because the code is way too messy. You probably already have past errors.

Another thing. Ideally, your code starts with <?php up there, and ends with ?> at the end. This business of opening and closing PHP at all times in the middle of the code does not exist. Either you use echo, or otherwise close and reopen PHP only in very large blocks of HTML.


Another solution:

Here's another technique: we do an array with all the companies, and look for ID when doing the table, taking the foreach of the companies in the middle of the other loop (and breaking, running once):

<?php
    $listar = new FuncionarioDAO();
    $resultTudo = $listar->listar();

    $results = new FuncionarioDAO();
    $results = $results->retornaEmpresa();

    $empresas = array();
    foreach( $results as $values ) {
       $empresas[ $values['id'] ] = $values['nome'];
    }

    while($row = $resultTudo->fetch_assoc()) {?>
        <tr>
            <td><?= $row['id']?></td>
            <td><?= $empresas[ $row['id_empresa'] ]?></td>
            <td><?= $row['nome']?></td>
            <td><?= $row['sobrenome']?></td>
            <td><?= $row['telefone']?></td>
            <td><a class="label label-warning" href="funcionario-atualizar?x=<?= $row['id']; ?>">editar</a></td>
            <td><a href="funcionario-remover?x=<?= $row['id']; ?>" class="label label-danger">remover</a></td>
        </tr>
    <?php }if($resultTudo->num_rows == 0){
        print("<h3 class='btn btn-warning'>Nenhum produto cadastrado ainda</h1>");
    }?>
    
03.12.2014 / 16:13