Replace id by table name

Asked

Viewed 421 times

0

So guys, I have this table with information like: id, student, computer, date of registration, time of entry, time of departure and reason, but in student and computer I want to show the name/ description and not the id. Follow my code there.

Student and computer are from another table

public function buscaTodos() {
    $sql = "SELECT * FROM " . $this->tabela;
    $sqle = $this->con->query($sql);

    $dados = $sqle->fetchAll(PDO::FETCH_ASSOC);
    return $dados;
}
$registro = new Registro();
$dados = $registro->buscaTodos();


<table class='table][1]][1] table-bordered table-striped'>
        <tr>
            <th>ID</th>
            <th>Aluno</th>
            <th>Computador</th>
            <th>Data</th>
            <th>Hora de Entrada</th>
            <th>Hora de Saída</th>
            <th>Motivo</th>
            <th>Opções</th>
        </tr>

    <?php

    foreach($dados as $r){

        $id=$r['id'];
        $aluno=$r['id_aluno'];
        $computador=$r['id_computador'];
        $dt_registro = $r['dt_registro'];
        $hr_entrada = $r['hr_entrada'];
        $hr_saida = $r['hr_saida'];
        $motivo = $r['motivo'];



    ?>
        <tr>
            <td><?=$id?></td>
            <td><?=$aluno?></td>
            <td><?=$computador?></td>
            <td><?=$dt_registro?></td>
            <td><?=$hr_entrada?></td>
            <td><?=$hr_saida?></td>
            <td><?=$motivo?></td>
        </tr>

    <?php } ?>
    </table>

aparece assim

  • What is the question?

  • young person, if the fields Name and description are in the same table, just change in $r['id_student'] for $r['student name'] for example, the code is very simple but you need to know the structure of the table

  • are in another table

  • I don’t think the fields aluno and computador are populated in this table. If they are in another, you will need to do a join, perhaps using the id of aluno.

1 answer

1

Hello, good, you want to do a somewhat complex search in your database, using the student and computer id to refer to their respective tables, taking their name.

For this you should do a somewhat complex query too, using Join. and on. I don’t know how your database is modeled, but I have a query similar to what you want, which can serve as a basis to create yours. follows the code:

function buscaProjetoPorUsuario($id_usuario){
        $select = $this->conexao->prepare("SELECT p.id, p.nome, p.descricao, p.dono, p.id_usuario,p.data, p.porc_marketing, p.porc_site, p.porc_desing, u.nome FROM projetos P join usuarios U on (P.id_usuario = U.id) WHERE U.id = '$id_usuario'");
        $select->setFetchMode(PDO::FETCH_ASSOC);
        $select->execute();
        $projetos = $select->fetchAll();
        return $projetos;
    }

Now I will explain the query to you, excerpt by excerpt.

The purpose of this query is to search for the information of a project, and to search for the name of a user using the id_usuario, to reference the user table.

first I define everything I want to search using:

SELECT p.id, p.nome, p.descricao... and u.nome

then I give a FROM projetos P, usuarios U on (P.id_usuario = U.id)

Here I am saying that when I use the letter P, in p.id, p.descricao... I am referring to the Projects table, and when I use the letter U I am referring to the Users table, then I say on (P.id_usuario = U.id), this causes the query to relate to the Usuarios table, using the id_usuario field, to relate to the id field of the User table.

I hope you understand, if you need help, share the structure of your tables in the database, so we can help you assemble a query.

Browser other questions tagged

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