I am doing my project to graduate, it is a web admin of works and I am looking for how to insert in the WORKS table the materials with the quantities (obras_cant)
and the tools with their quantities (herra_cant)
, example: Gray paint-15 Gal, sand m2, emery 2 Unds. In this case "gray paint" I already bring it from the table materiales
, as well as "emery" from the table herramientas
to later show the works in lists with their materials and tools in pdf to be checked at the time of delivery. a teacher that it was better to do it by placing the LONGTEXT field and inserting everything with JSON so that it saves the one id
for each tool or material with its quantities respectively. but I have no idea how to do it. Until now I am showing the data to consult to select with checkbox ......
<?php include_once 'templates/header-internas.php';
require_once('funciones/bd_conexion.php');
?>
<section class="seccion contenedor">
<h2>Agregar Obra</h2>
<form action="validar_obras.php" class="login" method="POST" id="obras">
<div class="campo">
<label for="obra">Nombre: </label>
<input type="text" name="obra" id="obra" placeholder="Introducir obra">
</div>
<div class="campo">
<label for="compra">Numero de Orden compra: </label>
<input type="text" name="compra" id="compra" placeholder="Introducir">
</div>
<div class="campo">
<label for="user">Supervisor a Cargo:</label>
<?php
try {
$sql = "SELECT * FROM usuarios WHERE status_id = 3 ";
$res_user = $conn->query($sql);
echo "<select name='user'>";
while ($user = $res_user->fetch_assoc()) { ?>
<option value="<?php echo $user['id_user'] ?> ">
<?php echo $user['nombre_user'] . " " . $user['apellido_user']; ?>
</option>
<?php }
echo "</select>";
} catch (Exception $error) {
echo "Error:" . $error->getMessage();
}
?>
</div>
<div class="campo clearfix">
<label for="client">Cliente </label>
<?php
try {
$sql = "SELECT * FROM clientes ";
$res_client = $conn->query($sql);
echo "<select name='client'>";
while ($client = $res_client->fetch_assoc()) { ?>
<option value="<?php echo $client['id'] ?> ">
<?php echo $client['nom_cliente'] . " " . $client['planta']; ?>
</option>
<?php }
echo "</select>";
} catch (Exception $error) {
echo "Error:" . $error->getMessage();
}
?>
</div>
<div class="campo clearfix">
<label for="herram">Selecionar herramientas:</label><br/>
<?php
try {
$sql = "SELECT * FROM herramienta ";
$res_herram = $conn->query($sql);
while ($herram = $res_herram->fetch_assoc()) {
echo '<input type="checkbox" name="herram[]" value=' . $herram['id_herra'] . '> ' . $herram['nom_herra'] . " " ;
echo '<input type="number" min="0" max="5" id="cant_herram" size="1" name="herram_cant" placeholder="0">' . '<br/>';
}
} catch (Exception $error) {
echo "Error:" . $error->getMessage();
}
?>
</div>
<div class="campo desplegable clearfix">
<label for="mater">Selecionar materiales:</label><br/>
<?php
try {
$sql = "SELECT * FROM materiales ";
$res_mater = $conn->query($sql);
while ($mater = $res_mater->fetch_assoc()) {
echo '<label><input type="checkbox" name="mater[]" value=' . $mater['id_mat'] . '> ' . $mater['nom_mat'] . " " ;
echo '<input type="number" min="0" max="50" id="cant_mat" size="1" name="mat_cant[]" placeholder="0">' . '<br/>';
}
} catch (Exception $error) {
echo "Error:" . $error->getMessage();
}
?>
</div>
<div class="campo">
<input type="submit" id="agregar" name="submit" class="button" value="Agregar">
</div>
</form>
I've already solved the dilemma and I'll publish it for someone who wants to do it and save themselves problems of creating extra code or saving many tables in their DB. When saving in the database, insert the WORKS and TOOLS with their quantities through the function
JSON_ENCODE
calling the functions$herramientas = herramientas_json($herram, $cant_h); $materiales = materiales_json($mater, $cant_m);
, a piece of code where the call to the functions is made and then the insertion to the DBfrom there it goes to
funciones.php
which it performs the conversion toJson
also deconversion withJSON_DECODE
. conversion firstAfter the insertion procedure is done, the data is saved as follows, observing the fields
mat_cant
andherra_cant
inserting the data with the format,json
but if you see the code above,materiales_json
I use the functionarray_combine
to join theid
materials or tools according to the function and save a shorter json. Then to display and format the saved data I did it as followsIf you review carefully in the
<td>
materials and also in tools, the call to the functions is madeformatear_materiales
andformatear_herramientas
from there it goes tofunciones.php
and performs thejson_decode
With that and the sql cycles, foreach and while, queries are made to the material tables and tools to show everything as shown in the following image
I hope it helps you, and if I'm wrong and you can improve the code, PLEASE don't hesitate to write your comments.