I have in the model the following function that I want to introduce in the database:
public function agregarBolsasModel($datos, $tabla){
$stmt = Conexion::conectar() -> prepare("INSERT INTO $tabla(lote, caja, bolsa)VALUES(:uno1, :dos2, :tres3)");
$stmt -> bindParam(":uno1", $datos["loteNum"], PDO::PARAM_INT);
$stmt -> bindParam(":dos2", $datos["cajaNum"], PDO::PARAM_INT);
$stmt -> bindParam(":tres3", $datos["valoresBox"], PDO::PARAM_INT);
if($stmt->execute()){
return "ok";
}else{
return "error";
}
$stmt->close();
}
The question that the third value or ": three3" is a data array that happened with the following array: (It goes to a controller, but I omit it here)
public function agregarEnCaja(){
$datos = array("loteNum" => $this -> loteIngreso, //101
"cajaNum"=> $this -> cajaIngreso, //1
"valoresBox"=> $this -> valoresCaja); //[1,2,3]
$respuesta=$respuesta=GestorOIT::agregarBolsasController($datos);
echo $respuesta;
}
How can I iterate element 3 so that it enters the data depending on its size, for example, the data array is like this:
$datos={loteNum:101, cajaNum:1, valoresBox[1,2,3]}
I want it to stay in the function like this, depending on its size:
INSERT INTO tabla(lote, caja, bolsa)VALUES(101,1,1);
INSERT INTO tabla(lote, caja, bolsa)VALUES(101,1,2);
INSERT INTO tabla(lote, caja, bolsa)VALUES(101,1,3);
Any help would be greatly appreciated.
EDITED
I tried this way but nothing
public function agregarBolsasModel($datos, $tabla){
$datosL=array($datos["valoresBox"]);
for($p=0; $p<sizeof($datosL); $p++){
$stmt = Conexion::conectar() -> prepare("INSERT INTO $tabla(lote, caja, bolsa)VALUES(:uno1, :dos2, :tres3)");
$stmt -> bindParam(":uno1", $datos["loteNum"], PDO::PARAM_INT);
$stmt -> bindParam(":dos2", $datos["cajaNum"], PDO::PARAM_INT);
$stmt -> bindParam(":tres3", $datosL[$p], PDO::PARAM_INT);
$stmt->execute();
}
$stmt->close();
}
Use PHP's count($array) function to get the size of an array: Change
by
being that way:
In the end I was able to solve it as follows: