How to Improve/Optimize this code?

Asked

Viewed 44 times

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.

2 answers

3


I used the counter loop to concatenate with the string that represents the key of the received request array.

$user = $_SESSION['nome_usuario'];
$data = date('Y-m-d H:i:s');
$lotes = $this->request->data();

for ($i = 0; $i < count($lotes); $i++) {
    $keyLote = 'lote';
    $keyLoteQtd = 'qtdLote';

    if ($i > 0) {
        $keyLote = $keyLote . $i;
        $keyLoteQtd = $keyLoteQtd . $i;
    }

    $lote = $lotes[$keyLote];

    if ($lote != '') {
        $loteQtd = $lotes[$keyLoteQtd];
        $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);
    }
}

Avoid using the function empty() to check if variables of type string are empty (Type comparison). See the following case:

$foo = 'null';
$bar = '0';

var_dump(empty($foo)); //false
var_dump(empty($bar)); //true
  • 1

    because as he spoke as the batch number grows he would have to implement something more in the array namely 'lote7' => 'qtdLote7' etc...

  • 1

    I did not pay attention to this detail, I will update the answer. Thanks for the observation!

  • 1

    @Scrapbench, updated the answer! Thanks xD

  • 1

    Very clean code very good

1

Good afternoon Alex I would recommend to iterate through an array and create these querys dynamically as an example

if(!isset($data))
   {
     return;
   }


$totalDeDataLotes = count($data);

for ($i=0; $i < $totaldelotes; $i++) {
  if ($i == 0) {
      $lote[$i] = $this->request->data['lote'];  
  }
  $lote[$i] = $this->request->data['lote'.$i]; 
}

// a partir de aqui o seu $i sera a quantidade maxima de elementos na sua array $lotes
$a = 0;

while ($a <= $i) {
  if(!empty($lote[$i])){
    if ($a == 0) {
        $loteQtd = $this->request->data['qtdLote'];
    }

    $loteQtd = $this->request->data['qtdLote'.$a];
    $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[$i] = NULL;
  }
$a++
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.