I'm doing a facturer, and when it came to billing several products, it occurred to me to use checkboxs
since I still have not gotten with JS
, so is the code of the checkboxs
:
<?php
require_once ("ModeloProductos.php");
$consulta = new Productos();
foreach ($consulta->mostrar_productos() as $registro):?>
<tr>
<td style="text-align: center"><?php echo $registro->Codigo?></td>
<td style="text-align: center"><?php echo $registro->producto?></td>
<td style="text-align: center"><?php echo $registro->precio . " BsF "?></td>
<td><input type='checkbox' name ='codigo[]' value='<?php echo $registro->Codigo?>'></td>
<input type="hidden" value="24" name="acceso">
</tr>
<?php
endforeach;
?>
Each checkbox
will have the code of that product, the problem happens when I select more than 1, I pass the whole arrangement, I go through the sentence and it only returns the last checkbox
selected and not the others, here the code:
$arreglo = $_POST["codigo"];
$cantidad = count($arreglo);
require_once ("Conexion.php");
$conexion = Conexion::conectar();
for ($i = 0 ; $i < $cantidad ; $i++)
{
$consulta = $conexion->prepare("SELECT producto, precio FROM productos WHERE Codigo = $arreglo[$i]");
$consulta->execute();
$registro = $consulta->fetchAll(PDO::FETCH_OBJ);
}
foreach ($registro as $item)
{
echo $item->producto;
echo $item->precio;
}
There, in theory, what I would be doing is a cycle to make the query, where I would select the product and the price where the code was equal to the code in that position, but only take me the last checkbox
selected.
A clearer example to understand me better: I have three checkbox
, one code each, I select the three, at the time of invoicing I should return the product and the price of those three checkboxs
selected, but only returns me the last checkbox
.