Show name of a record using the ID found in another table [closed]

1

I have two tables:

Personal: cedula, rango_id

Ranges: range_id, range (field with range description)

I would like you to modify the staff where your rank is selected your ID would not appear, like, 1 2 3 4, but the description that each one has in the table would appear.

I have the following code, but I do not know how to show it. Any ideas to do this?

   <script>
function confirmSav()
{
  var agree=confirm("¿Desea modificar este registro? ");
  if (agree)
  return true ;
else
   return false ;
}
</script>

<script>
function confirmDel()
{
  var agree=confirm("¿Desea eliminar este registro? ");
  if (agree)
  return true ;
else
   return false ;
}
</script>
<html>
<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="/sino.js"></script>
    <title> </title>
</head>
<body>
<form method = "POST" action = "">
<h1 align ="center">Bienvenido</h1>
<a href ="index.html"><ol>Pagina Principal</ol></a>
<a href ="registrar.php"><ol>Registrar</ol></a>
<a href ="listado.php"><ol>Listado</ol></a>
<a href ="Sancion.php"><ol>Sancion</ol></a>
<h1>Listado<br></h1>
<?php
include "conexion.php";
global $cone;
$contador = 0;
$registros=mysqli_query($cone,"select * from personal");
while ($reg = mysqli_fetch_array($registros))
{
$contador=$contador+1;
echo $contador."<br>";
echo "NOMBRE: ".$reg['nombre']."<br>";
echo "APELLIDO: ".$reg['apellido']."<br>";
echo "CEDULA: ".$reg['cedula']."<br>";
$registros=mysqli_query($cone,"select * from personal");

$sql =mysqli_query($cone,"SELECT a.cedula, b.rango_id, b.rango
FROM   personal a 
LEFT JOIN rangos  b ON a.rango_id = b.id_rango
ORDER BY a.cedula ASC");
$reg = mysqli_fetch_array($registros);

echo "RANGO: ".$reg['rango_id']."<br>";
echo "<a onClick='return confirmSav();' href=actualizar.php?cedula=".$reg['cedula']." >editar<a/>"."<br>";
echo "<a onClick='return confirmDel();' href=procesar3.php?cedula=".$reg['cedula']." >eliminar<a/>"."<br>";
echo "<br>";
echo "<br>";
}
?>
</body>
</html>
  

Note, I do not know if I should add this line to the code:

$sql =mysqli_query($cone,"SELECT a.cedula, b.rango_id, b.rango
FROM   personal a LEFT JOIN rangos  b ON a.rango_id = b.id_rango
ORDER BY a.cedula ASC");
$sql2 = mysqli_fetch_array($sql);
echo "RANGO: ".$sql2['rango']."<br>";
    
asked by Victor Alvarado 18.01.2017 в 16:32
source

1 answer

1

You are taking RANGO_ID of the RANGOS table, where that value belongs to PERSONAL , so you explain at the beginning. Simply change this value b.rango_id by a.rango_id of this line of code:

$sql =mysqli_query($cone,"SELECT a.cedula, b.rango_id, b.rango

FROM   
    personal a 
LEFT JOIN 
    rangos  b ON a.rango_id = b.id_rango
ORDER BY 
    a.cedula ASC");

$reg = mysqli_fetch_array($registros);

Running:

$sql =mysqli_query($cone,"SELECT a.cedula, a.rango_id, b.rango
FROM   
    personal a 
LEFT JOIN 
    rangos  b ON a.rango_id = b.id_rango
ORDER BY 
    a.cedula ASC");
$reg = mysqli_fetch_array($registros);
    
answered by 18.01.2017 / 17:19
source