How to select several records with Checkbox?

4

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 .

    
asked by Shum 29.03.2018 в 04:57
source

3 answers

4

Since you are apparently using PDO to access the database, there are a couple of things to correct (in addition to the problem of the original question):

1) It does not make sense to use prepare within a cycle, it must run outside of it.

2) When using prepare it is incorrect to concatenate a variable within the string of the query. Placeholders must be used.

3) Then, within the cycle for execute the query prepared assigning the value to the placeholder.

4) If the field codigo is unique for each checkbox (I guess it will be key to the table) it is not necessary to use fetchAll since you will never bring more than one record, with fetch reaches.

5) And now, by solving the question, you should declare the variable $registros out of the cycle as an array and within it load each object obtained with the query.

$consulta = $conexion->prepare("SELECT producto, precio FROM productos WHERE Codigo = :CODIGO");

$registros = array();

for ($i = 0 ; $i < $cantidad ; $i++) {
    $consulta->execute(array(
        ':CODIGO' => $arreglo[$i]
    ));
    $registros[] = $consulta->fetch(PDO::FETCH_OBJ);
}

// $registros es un array de objetos:
foreach ($registros as $item) {
    echo $item->producto;
    echo $item->precio;
}
    
answered by 29.03.2018 в 18:07
3

the solution is very simple, the only problem is that in the for you are overwriting the values of the products in the $ register variable:

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); //<=aqui
}

That's why you only get the values of the last record, when the correct thing is that you should declare the array $ register and place the values of the products as if it were a vector:

$registro=array(); //declarar el array 

for ($i = 0 ; $i < $cantidad ; $i++) {

    $consulta = $conexion->prepare("SELECT producto, precio FROM productos WHERE Codigo = $arreglo[$i]");
    $consulta->execute();
    $registro[$i] = $consulta->fetchAll(PDO::FETCH_OBJ); //<=aqui te falta el [i]
}

This way you already have all the records to do the foreach. I hope you find it helpful, greetings.

    
answered by 29.03.2018 в 17:17
0

I would believe that your error is in the $ registry variable since it is not declared outside of the for, well that is the first the second is that every time the cycle is executed for the same variable $ record is rewritten that is to say the previous object to create the new one, try to try this code:

$arreglo = $_POST["codigo"];
$cantidad = count($arreglo);

require_once ("Conexion.php");

$conexion = Conexion::conectar();
$registro = array();

for ($i = 0 ; $i < $cantidad ; $i++) {

    $consulta = $conexion->prepare("SELECT producto, precio FROM productos WHERE Codigo = $arreglo[$i]");
    $consulta->execute();
    array_push($registro,$consulta->fetchAll(PDO::FETCH_OBJ));
}

foreach ($registro as $item) {

    echo $item->producto;
    echo $item->precio;
}

What it does is that it initializes the $ register variable as an array then in the for cycle we only add the new objects.

    
answered by 29.03.2018 в 07:38