Doubt when searching for a foreach id in php

Asked

Viewed 354 times

-1

Good evening guys, I’m developing a course completion work code and I have a question. I have a table users and vehicles, use Inner Join in select and then display them using foreach. However, in each field the user can choose or change the user or delete it (send the data according to the id selected using SESSION). 1] But here’s the problem! When I try to pass the id code I want to modify, it just takes the last id instead of the respective code. For example: I want to delete code 1, hit delete, the system deletes code 3 instead of 1.

How my display is:

<table class="table">
    <tr>
        <th>Código</th>
        <th>Nome</th>
        <th>Telefone</th>
        <th>Tipo</th>
        <th>Placa</th>
        <th>Modelo</th>
        <th>Cor</th>
        <th>Fabricante</th>
    </tr>
    <?php foreach ($dados as $linha):
        $id = $linha[0];
    ?>
    <tr>
        <td><?php echo $linha[0]; ?></td>
        <td><?php echo $linha[1]; ?></td>
        <td><?php echo $linha[2]; ?></td>
        <td><?php echo $linha[3]; ?></td>
        <td><?php echo $linha[4]; ?></td>
        <td><?php echo $linha[5]; ?></td>
        <td><?php echo $linha[6]; ?></td>
        <td><?php echo $linha[7]; ?></td>
        <td><a name="altera" onclick="
         <?php
            $_SESSION['id'] = $id;
            $_SESSION['alterar'] = 'usuario';
          ?>" href="alterar.php">Alterar</a></td>
        <td><a name="excluir" onclick="
         <?php
            $_SESSION['excluir'] = 'usuario';
         ?>" href="processaexcluir.php">Excluir</a></td>
    </tr>
    <?php 
        }
        $id++;
        endforeach;
    ?>
    </table>

I wonder if there is any way to search for a specific index in the foreach or if there is some other way to do it. Thank you for your attention.

  • try to use just one, so: <td><? php echo $line; ? ></td> delete the others, no need to loop.

1 answer

0


Xubilau, the problem is that the global variable '$_SESSION['id'] = $id;' is always being assigned to the last id, and so is having this error, try using php’s GET.

<table class="table">
    <tr>
        <th>Código</th>
        <th>Nome</th>
        <th>Telefone</th>
        <th>Tipo</th>
        <th>Placa</th>
        <th>Modelo</th>
        <th>Cor</th>
        <th>Fabricante</th>
    </tr>
    <?php foreach ($dados as $linha):
        $id = $linha[0];
    ?>
    <tr>
        <td><?php echo $linha[0]; ?></td>
        <td><?php echo $linha[1]; ?></td>
        <td><?php echo $linha[2]; ?></td>
        <td><?php echo $linha[3]; ?></td>
        <td><?php echo $linha[4]; ?></td>
        <td><?php echo $linha[5]; ?></td>
        <td><?php echo $linha[6]; ?></td>
        <td><?php echo $linha[7]; ?></td>
        <td><a name="altera" href="alterar.php?CarroId=<?php echo $linha[0]; ?>">Alterar</a></td>
        <td><a name="excluir" href="processaexcluir.php?CarroId=<?php echo $linha[0]; ?>">Excluir</a></td>
    </tr>
    <?php 
        }
        $id++;
        endforeach;
    ?>
    </table>

After that, in the PHP file, read the value that was passed by the URL using GET this way:

<?php
$carroId = $_GET['CarroId'];

...
?>

  • 1

    Thanks for the help, I tried to make changes to the code to see if I could do otherwise using Forms in change and delete, but none was giving for errors in javascript that I was unaware of. Now using its modification I managed to make it work. Thank you very much.

Browser other questions tagged

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