Bring data from the table using an ID

0

Please, I need your help with this. I have this table in the view:

What I need is that in the ID column I get a list to select the IDs that I have in this database, and when selecting an ID I load the item and the unit price in the view.

    
asked by David Bucci 06.12.2018 в 00:23
source

2 answers

1

Approach: The options are created with a call to the php database. The idea is to add a class with the id to the corresponding elements in that way when you click on samples the <td> corresponding class id. Then we execute a script (I'll use JQuery since it's easier) than when I click the select show with a show();

Code:

<select id="seletp">
   <?php while($fila=mysqli_fetch_array($resultado)){ ?>
         <option value="<?= $fila["id"];?>"><?= $fila["id"];?></option>
   <?php 
     }
   ?>
</select>
<table id="tablel">
<thead>
  <tr>
   <td>Id</td>
   <td>Articulo</td>
   <td>Precio/Unidad</td>
   <td>Cantidad</td>
   <td>Total</td>
  </tr>
</thead>
<tbody>
  <tr>
   <?php while($fila=mysqli_fetch_array($resultado)){ ?>
         <td class="ej consulta-<?= $fila["id"];?>"><?= $fila["id"];?></td>
         <td class="ej consulta-<?= $fila["id"];?>"><?= $fila["articulo"];?></td>
         <td class="ej consulta-<?= $fila["id"];?>"><?= $fila["precioUnidad"];?></td>
         <td class="ej consulta-<?= $fila["id"];?>"><?= $fila["cantidad"];?></td>
   <?php 
     }
   ?>
  </tr>
</tbody>
</table>

Script

$(document).ready(function(){
  $(".ej").hide();
  $("#seletp").click(function(){
    var id=$("#seletp").val();
    var clase="consulta-"+id;
    $(".ej").hide();
    $("#tablel").find(clase).show();
  });
});
    
answered by 06.12.2018 / 00:57
source
0

@matahombres this is all it shows me:

I leave the code

<?php
    // Ejemplo de conexión a base de datos MySQL con PHP.
    //
    // Ejemplo realizado por Oscar Abad Folgueira: http://www.oscarabadfolgueira.com y https://www.dinapyme.com

    // Datos de la base de datos
    $usuario = "root";
    $password = "";
    $servidor = "localhost";
    $basededatos = "sistfacturacion";

    // creación de la conexión a la base de datos con mysql_connect()
    $conexion = mysqli_connect( $servidor, $usuario, "" ) or die ("No se ha podido conectar al servidor de Base de datos");

    // Selección del a base de datos a utilizar
    $db = mysqli_select_db( $conexion, $basededatos ) or die ( "Upps! Pues va a ser que no se ha podido conectar a la base de datos" );
    // establecer y realizar consulta. guardamos en variable.
    $fila = "SELECT id,articulo,precioUnidad FROM factura2";
    $resultado = mysqli_query( $conexion, $fila ) or die ( "Algo ha ido mal en la consulta a la base de datos");

    ?>



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

    <select id="seletp">
   <?php while($fila=mysqli_feth_array($resultado)){ ?>
         <option value="<?= $fila["id"];?>"><?= $fila["id"];?></option>
   <?php 
     }
   ?>
</select>
<table id="tablel">
<thead>
  <tr>
   <td>Id</td>
   <td>Articulo</td>
   <td>Precio/Unidad</td>
   <td>Cantidad</td>
   <td>Total</td>
  </tr>
</thead>
<tbody>
  <tr>
   <?php while($fila=mysqli_feth_array($resultado)){ ?>
         <td class="ej consulta-<?= $fila["id"];?>"><?= $fila["id"];?></td>
         <td class="ej consulta-<?= $fila["id"];?>"><?= $fila["articulo"];?></td>
         <td class="ej consulta-<?= $fila["id"];?>"><?= $fila["precioUnidad"];?></td>
         <td class="ej consulta-<?= $fila["id"];?>"><?= $fila["cantidad"];?></td>
   <?php 
     }
   ?>
  </tr>
</tbody>
</table>

<script>


    $(document).ready(function(){
  $(".ej").hide();
  $("#seletp").click(function(){
    var id=$("#seletp").val();
    var clase="consulta-"+id;
    $(".ej").hide();
    $("#tablel").find(clase).show();
  });
});
</script>


</body>
</html>


<?php 


    mysqli_close( $conexion );


 ?>

Where do you think this is the problem?

    
answered by 06.12.2018 в 01:55