List of Students in PHP and MySQL

2

I am developing a student registration system, they have a career, semester, period. These attributes are stored in the student table as an id, race_id, semester_id, period_id, and represent a data from another table, since I have selections loaded into tables. I have a list that works with this code:

function bd_alumno_datos_todos(){
$sql = "SELECT 
           cedu_alum, carr_id, seme_id, pera_id, peri_id, capa_id
        FROM 
            alumno
        ORDER BY cedu_alum ASC";

$datos = sql2array( $sql );

return $datos;
}

It shows all the data of the student, but it happens that it shows the aforementioned ids, where career goes 3, semester 4, period 6, and so on, I would like to know how to do so that by showing the list, each value ID is changed by its respective value in the table, ie.

carrera_id = 3, will come out systems carrera_id = 2, education

Same for the others as I could do?

here the code of the list

<?php include 'conexion.php'; $alumno= bd_alumno_datos_todos(); <h2>Listado de Alumnos</h2> <table>     <thead>             <tr>
        <th><center>Cédula</th>
        <th><center>Carrera</th>
        <th><center>Semestre</th>
        <th><center>Período</th>
         <th><center>Capacitado</th>
         <th><center>Opciones</th>
    </tr>
</thead>

<tbody>    




    <?php foreach ($alumno as $alumno_temp): ?><tr>

        <td><?=$alumno_temp['cedu_alum']?></td>

         <td> <?= $alumno_temp['carr_id'];?></td>

        <td><?=$alumno_temp['seme_id']?></td>
        <td>
        <?=$alumno_temp['pera_id']?> 
        <td>

A friend told me that with something like this:

function bd_carrera_datos($id){
$sql = "SELECT 
            carr 
        FROM 
            carrera
        WHERE 
            carr_id = '$id' 
        LIMIT 1";
$datos = sql2row( $sql );
return $datos; }

that will take the value of the ID but it has not worked

try to do this in the column;

but only shows the race ID.

Thank you very much for your answers!

    
asked by Victor Alejandro Alvarado Vilo 26.05.2016 в 04:42
source

1 answer

2

I would not recommend the solution suggested by your friend because it is somewhat inefficient (it requires more time, connections, data ...). Instead you could fix it by doing a JOIN with the different tables where the names you want to show are saved.

Now you are doing SELECT only from the "student" table where you have foreign keys (race_id, semester_id, period_id) for other tables ([I guess] career, semester and period respectively). Then you could do a JOIN with those tables and get the name directly instead of the ID.

It would be something like this: (eye, it's just a demonstration: I have not tried it and I do not know all the names of the fields in your tables, so it may contain errors):

SELECT   a.cedu_alum, c.carr, s.seme, p.peri
FROM     alumno a
         LEFT JOIN carrera  c ON a.carr_id = c.carr_id
         LEFT JOIN semestre s ON a.seme_id = s.seme_id
         LEFT JOIN periodo  p ON a.peri_id = p.peri_id
ORDER BY a.cedu_alum ASC
    
answered by 26.05.2016 / 05:39
source