Do you doubt how to store the ID of a record brought from the bank for later use in PHP?

Asked

Viewed 96 times

-1

How could I store the ID of each record, and the ID_PESSOAS by going through the while ($exibe = mysqli_fetch_assoc($dados) so that in the future the user click on the "see vacancies" button I know which of the records brought by select was clicked by the user, and then I can take it to another page with all the data pertaining to the row of the registry in question in Mysql.

I’ll leave the essensial code part for understanding below

Selecting data from the database

session_start();

include('../conexao.php');

$redirecionar = $_SESSION['email'];

$consulta = "SELECT * FROM vagas_emprego
         LEFT JOIN pessoas ON `ID_PESSOAS` = `PESSOAS_ID_PESSOAS`
         WHERE `status` = 'O' AND `ramo` = 'Produção'
   UNION ALL             
        SELECT * FROM habilidades_candidatos
        LEFT JOIN pessoas ON `ID_PESSOAS` = `PESSOAS_ID_PESSOAS`
        WHERE `status` = 'O'AND `ramo` = 'Produção'";
$dados = mysqli_query($CONN, $consulta);
$row = mysqli_num_rows($dados);

Displaying the data

 <div class="col-md-10 ml-auto mr-auto"><br>
        <?php while ($exibe = mysqli_fetch_assoc($dados)) { ?>
            <div class="card border-dark mb-3">
                <div class="card-header">
                    Produção | Portal de Empregos
                </div>
                <div class="md-form">
                    <h4 class="card-title"><?php echo " " . $exibe["TITULO_VAGA"] . " | " . $exibe["ID_VAGAS_EMPREGO"]; ?></h4>
                    <textarea readonly style="background-color: white; resize: none" id="form7" class="md-textarea form-control" rows="10"> <?php echo $exibe["DESCRICAO_VAGA"]; ?></textarea>
                </div>
                <div class="card-footer">
                    <center><a href="#" class="btn btn-primary">Ver Vaga</a></center>
                </div>
            </div>
        <?php }
  • In the element <a> you enter the URL of the other page; just pass the id value through the query string, leaving something like /exibir.php?id=1

  • your SELECT wouldn’t that be so? SELECT * FROM Vacancies LEFT JOIN People ON vagas_emprego.ID_PESSOAS = pessoas .PESSOAS_ID_PESSOAS WHERE status = 'O' AND ramo = 'Production

2 answers

0

From what I understand need only id and store in a variable, I will give examples with PDO but ask to enter in mysqli as you want.

Example listing of all records:

$consulta = $this->query("SELECT * FROM vagas_emprego"); // Consulta sem relacionamento

Continuation of this relationship-free consultation

//Validação se tem registros listados caso o rowCount retorn maior que zero é por que ele tem registros
if($consulta->rowCount() > 0){
    $array = $consulta->fetchAll();
}
//retorno de todos dados
return $array;

Storing return data from all records:

//loop para listar todos registros do banco e atribuindo a uma váriavel com index vazio assim fara index de 0 até ... 
foreach($array as $dados){
    $id[] = $dados['id'];
}
// retornando em indexs cada id de cada registro
return $id;

If you need related data just do it with INNER JOIN, LEFT JOIN, RIGHT JOIN as your example shows.

0

You can use PHP sessions to store each clicked item.

After obtaining all the database data, create the session variable responsible for storing all the clicked items.

//...
$row = mysqli_num_rows($dados);

$_SESSION['itemsClicked'] = (empty($_SESSION['itemsClicked'])) ? array() : $_SESSION['itemsClicked'];

With the session variable created we need to add the vacancies that were clicked. This can be done on the vacancy view page and, as it was not informed which file, let’s assume that the vacancy description is in the file vaga.php.

In the list of vacancies we will assign the id of the wave in the attribute href:

<div class="card-footer">
    <center><a href="vagas.php?id=<?php echo $exibe['ID_VAGAS_EMPREGO'] ?>" class="btn btn-primary">Ver Vaga</a></center>
</div>

In the archive vagas.php we will assign the id of the wave clicked to our session variable.

//vagas.php

$vagaId = $_GET['id'];
if (isset($vagaId) && $vagaId != '') {
    $_SESSION['itemsClicked'][] = $vagaId;
}

Now just use the session variable to do the necessary manipulations.

EDIT

To store the two values and use them as a parameter it is necessary to make a small modification in the examples I passed.

First you need to add another value in the query string that will be the ID_PESSOAS.

<div class="card-footer">
    <?php $queryString = 'id=' . $exibe['ID_VAGAS_EMPREGO'] . '&idPessoas=' . $exibe['ID_PESSOAS']; ?>
    <center><a href="vagas.php?<?php echo $queryString ?>" class="btn btn-primary">Ver Vaga</a></center>
</div>

I put the contents of the query string in a variable for greater readability.

In the archive vagas.php it is necessary to identify and store the new value provided through the request.

//vagas.php

$vagaId = $_GET['id'];
$idPessoas = $_GET['idPessoas'];

//Realize as validações da string como mostrado acima

$_SESSION['itemsClicked'][] = [
    'id' => $vagaId,
    'idPessoas' => $idPessoas
];

Now each item of the array will have an array containing the id and the id of the people.

  • Perfect answer, you’re fod*a, thanks for the help!

  • @Bianca, give me nothing!

  • one more question if it’s not too much trouble, and how would I pass 2 attributes in this case?

  • @Bianca, what are these two attributes?

  • due to some needs I needed to change my select to: $query = "SELECT * FROM vacans_employment LEFT JOIN people ON ID_PESSOAS = PESSOAS_ID_PESSOAS WHERE status = 'O' AND ramo = 'Administration' UNION ALL SELECT * FROM habilities_candidates LEFT JOIN people ON ID_PESSOAS = PESSOAS_ID_PESSOAS WHERE status = 'O'AND ramo = 'Administration'";

  • And in this situation because I’m making two selects I can’t just take the ID of the record because in the other table there may be a record with the same ID, so I need to take the ID_PESSOAS and send along with the other ID that had already helped me, that I know so there will be no duplication of records at show time!

  • Makes a Edit in the question that I update the answer to you... And if the answer helped you don’t forget about that better response marking and that pop to strengthen kkk

  • OK I’ll change the select

Show 3 more comments

Browser other questions tagged

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