Go through array and check if a field is empty (PHP)

0

In one of the files I have a PivotTable which receives the data from BD , but with the possibility of inserting more rows in the table. Note that when I enter more rows the idFaturacao will be empty.

This is my table:

<TABLE id="dataTable">
<?php 
    $query_periodosFaturacao = mysqli_query($link,"SELECT * FROM faturacao WHERE fk_obras=$id ORDER BY num_fatura ASC");
    while($periodosFaturacao = mysqli_fetch_array($query_periodosFaturacao)) {
            echo "<TR>";
            echo "<TD width='100px;'><INPUT type='date' id='data_afaturar' name='data_afaturar[]' value='".$periodosFaturacao['data']."' required/></TD>";
            echo "<TD><input type='text' name='valor_afaturar[]' id='valor_afaturar' value='".$periodosFaturacao['valor']."' required>€</TD>";
            echo "<input type='hidden' name='idFaturacao[]' value='".$periodosFaturacao['id']."' />";
            echo "</TR>";
    }
    ?>
</TABLE>

In another file where I send the data to the database, the problem is here! When traversing array , elements that are empty make INSERT and elements where idFaturacao is filled it does UPDATE of info. I do not know how to check the empty and filled elements in the array.

$data_afaturar = $_POST['data_afaturar'];
$valor_afaturar = $_POST['valor_afaturar'];
$idFaturacao = $_POST['idFaturacao'];

foreach($data_afaturar as $a => $b){
    if(empty($idFaturacao[$a])){
        echo "INSERT";
    }
    else{
        echo "UPDATE";
    }
}

Thank you for your attention!

    
asked by anonymous 28.09.2017 / 11:30

1 answer

1

Por exemplo, se o array for assim :'1', '2', '', '4'. Just check if $ b is empty

Test clicking here

$idFaturacao = array("1", "2", "", "4");

foreach ($idFaturacao as $a => $b) {
    //$b = trim($b);
    if (empty($b))
        echo "indice $a -> $b faz INSERT <br>";
    else
        echo "indice $a -> $b faz UPDATE <br>";
}
  

With question code test here

$data_afaturar = array("a", "b", "c", "d");
$idFaturacao = array("1", "2", "", "4");


foreach($data_afaturar as $a => $b){
    if(empty($idFaturacao[$a])){
        echo "indice $a -> $b faz INSERT <br>";
    }
    else{
        echo "indice $a -> $b faz UPDATE <br>";
    }
}
    
28.09.2017 / 15:28