Values are not in the right column in the table

Asked

Viewed 105 times

0

erro

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

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

        while($row = $resultTudo->fetch_assoc()) {?>
            <tr>
                <td><?= $row['id']?></td>
                <?php
                foreach($results as $values) {?>
                    <td><?php if($values['id'] == $row['id_empresa']){echo $values['nome']; break;}?></td>
                <?php }?>
                <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>");
        }?>
  • 1

    There’s a foreach there in the middle of the table line that doesn’t make any sense. And it would be nice to replace this open and close PHP stack with "echo", so it makes it easier to read the code. Something else, use <?= is to ask your application to work in one place only. This is kind of complicated.

  • There is an employee table q relates to a company table, the employee table has the company id then I put the company name to the list. But so was the table.

  • select * from employee Join company

  • I did to go through the result to find the value and print.

  • Could do $results[0]['nome'] and take out the foreach completely, since you want to get only one result, but anyway you could bring this field of a JOIN, with only one query. Take a look at the table overall, should have a <tr> with <td> more than others <tr>'s.

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 patching it up, because the code is too messed up. There are probably previous bugs already.

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


Another solution:

Here is another technique: we make a array with all companies, and we search for ID when making the table, taking the foreach of the enterprises in the middle of the other loop (and break, running once only):

<?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>");
    }?>
  • Okay. I’m gonna use it like this now. But why were you making that mistake?

  • 1

    You were creating a lot of TD inside the foreach. You were creating empty TD until you reached the company ID. But seriously, your code shouldn’t even need this. It had to be just <td><?= $row['nome_empresa']?></td>, just in place of the select * you put the right name on things, and an ON on the Join. It.

  • Ahhhhhh was supposed to have put before the foreach. Attention error :(

  • 1

    There wasn’t supposed to be any foreach. It was supposed to be just select id, empresa.nome AS nome_empresa, funcionario.nome AS nome, sobrenome, telefone from funcionario join empresa on id=id_empresa. Or something like that. And in place of foreach would suffice <td><?= $row['nome_empresa']?></td>

  • Yeah, only I’m using a query for everything. Ai needed the foreach.

  • 1

    Failure to use JOIN correctly. Seriously, if you learn to use it, it will help your code. Join with ON clause will bring only the right company, without any foreach.

  • But if I put like you said, then I’d need a foreach to go through and list.

  • 1

    I don’t know where you got this idea, but I wouldn’t have to. The ON of Join is for you to choose which will be the criterion of choice.

  • No? You can put an example of code?

  • 2

    See here about joins. http://answall.com/a/6448/70 - What changes in your case is that in the place of profession you will use company. And it is probably LEFT JOIN that interests you, because it will show all the names (table A), having or not company (table B). Once done JOIN, pro PHP is like a single table.

  • 1

    @Paulocosta see another way to do, without Join and without foreach, in the answer

  • Okay! I’ll look there Master Supreme.

  • Again this, no... hehehe.

Show 8 more comments

Browser other questions tagged

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