0
I have the following code, and I would like to know if it has how to improve, and how to improve this code:
$user = $_SESSION['nome_usuario'];
$data = date('Y-m-d H:i:s');
$lote = $this->request->data['lote'];
$lote1 = $this->request->data['lote1'];
$lote2 = $this->request->data['lote2'];
$lote3 = $this->request->data['lote3'];
$lote4 = $this->request->data['lote4'];
$lote5 = $this->request->data['lote5'];
$lote6 = $this->request->data['lote6'];
if(!empty($lote)){
$loteQtd = $this->request->data['qtdLote'];
$query = 'INSERT INTO lotes_mp (lote_nota, lote_nota_for, lote_item, lote_mp_id, lote_mp_qtd, lote_id, map_data, map_usuario)
VALUES ('."'".$id."'".','."'".$fornecedor."'".','."'".$item."'".','."'".$produto."'".','."'".$loteQtd."'".','."'".$lote."'".','."'".$data."'".','."'".$user."'".')';
$this->LotesMp->query($query);
}else{
$lote = NULL;
}
if(!empty($lote1)){
$loteQtd = $this->request->data['qtdLote1'];
$query = 'INSERT INTO lotes_mp (lote_nota, lote_nota_for, lote_item, lote_mp_id, lote_mp_qtd, lote_id, map_data, map_usuario)
VALUES ('."'".$id."'".','."'".$fornecedor."'".','."'".$item."'".','."'".$produto."'".','."'".$loteQtd."'".','."'".$lote1."'".','."'".$data."'".','."'".$user."'".')';
$this->LotesMp->query($query);
}else{
$lote1 = NULL;
}
if(!empty($lote2)){
$loteQtd = $this->request->data['qtdLote2'];
$query = 'INSERT INTO lotes_mp (lote_nota, lote_nota_for, lote_item, lote_mp_id, lote_mp_qtd, lote_id, map_data, map_usuario)
VALUES ('."'".$id."'".','."'".$fornecedor."'".','."'".$item."'".','."'".$produto."'".','."'".$loteQtd."'".','."'".$lote2."'".','."'".$data."'".','."'".$user."'".')';
$this->LotesMp->query($query);
}else{
$lote2 = NULL;
}
if(!empty($lote3)){
$loteQtd = $this->request->data['qtdLote3'];
$query = 'INSERT INTO lotes_mp (lote_nota, lote_nota_for, lote_item, lote_mp_id, lote_mp_qtd, lote_id, map_data, map_usuario)
VALUES ('."'".$id."'".','."'".$fornecedor."'".','."'".$item."'".','."'".$produto."'".','."'".$loteQtd."'".','."'".$lote3."'".','."'".$data."'".','."'".$user."'".')';
$this->LotesMp->query($query);
}else{
$lote3 = NULL;
}
if(!empty($lote4)){
$loteQtd = $this->request->data['qtdLote4'];
$query = 'INSERT INTO lotes_mp (lote_nota, lote_nota_for, lote_item, lote_mp_id, lote_mp_qtd, lote_id, map_data, map_usuario)
VALUES ('."'".$id."'".','."'".$fornecedor."'".','."'".$item."'".','."'".$produto."'".','."'".$loteQtd."'".','."'".$lote4."'".','."'".$data."'".','."'".$user."'".')';
$this->LotesMp->query($query);
}else{
$lote4 = NULL;
}
if(!empty($lote5)){
$loteQtd = $this->request->data['qtdLote5'];
$query = 'INSERT INTO lotes_mp (lote_nota, lote_nota_for, lote_item, lote_mp_id, lote_mp_qtd, lote_id, map_data, map_usuario)
VALUES ('."'".$id."'".','."'".$fornecedor."'".','."'".$item."'".','."'".$produto."'".','."'".$loteQtd."'".','."'".$lote5."'".','."'".$data."'".','."'".$user."'".')';
$this->LotesMp->query($query);
}else{
$lote5 = NULL;
}
if(!empty($lote6)){
$loteQtd = $this->request->data['qtdLote6'];
$query = 'INSERT INTO lotes_mp (lote_nota, lote_nota_for, lote_item, lote_mp_id, lote_mp_qtd, lote_id, map_data, map_usuario)
VALUES ('."'".$id."'".','."'".$fornecedor."'".','."'".$item."'".','."'".$produto."'".','."'".$loteQtd."'".','."'".$lote6."'".','."'".$data."'".','."'".$user."'".')';
$this->LotesMp->query($query);
}else{
$lote6 = NULL;
}
Basically it records each batch in a row in the database, recording a maximum of 7 batches, for 7 batches until it is not very extensive, but if it is necessary to record 20 - 30 batches, it would have hundreds of lines.
because as he spoke as the batch number grows he would have to implement something more in the array namely 'lote7' => 'qtdLote7' etc...
– ScrapBench
I did not pay attention to this detail, I will update the answer. Thanks for the observation!
– Victor Carnaval
@Scrapbench, updated the answer! Thanks xD
– Victor Carnaval
Very clean code very good
– ScrapBench