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

Asked

Viewed 753 times

0

In one of the files I have a dynamic table that receives data from BD, but with the possibility of inserting more rows in the table. Note that when inserting 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! array, the elements that are empty it makes the INSERT and the elements in which idFaturacao is filled it does the UPDATE info. I don’t 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!

  • No time you declared the variable $id_faturacao, that seems to be wrong.

  • Francisco the variable was declared here: $idFaturacao = $_POST['idFaturacao'];

  • No, it wasn’t. They’re different names, variable names should be strictly equal. See more here.

  • Francisco, you’re right had an underscore the most. Anyway does not correct the situation.

  • You have a variable $idFaturation and within the foreach you have another variable $idbilling with the minute f

  • Your code is very meaningless, in the foreach you iterate on the $data_afaturar and in checking, you’re checking the $idFaturação.

  • Francisco in the foreach I have the $data_afaturar because this is an element that is never empty, so I want to check is when the $idFaturation is!

  • Got it. And php is claiming some error?

  • How are the fields that will be used to insert new data in HTML? In the question you put only the lines that are generated for the existing records. And as Francis asked, does any error appear? Which?

  • Hello no error appears. just don’t do what I need you to do. That is the array has several elements, but some are empty. What I need is that it goes through the array and inside this one, the elements that have empty it do 'INSERT' and in the array the positions that have data it does 'UPDATE'. I don’t know if I made myself clear... For example, if the array looks like :'1', '2', '', '4'. It does the UPDATE of '1', '2' and '4', and in what is empty 'it does an INSERT

  • Joana, and how is the HTML of this field that comes with the id empty? Whereas what you posted in the question is just one loop of the bank records, there will always be a id associated. Is there any other form that the user can enter new data?

Show 6 more comments

1 answer

1

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

Test by 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>";
    }
}
  • But it’s not the value of $data_afarurar that it is necessary to verify, but the value in $idFaturacao.

  • For example, if the array looks like :'1', '2', '', '4'.

  • 1

    Exact. The $data_afaturar comes as ["a", "b", "c", "d"] and the $idFaturacao as ["1", "2", "", "4"]. What needs to be done is that if the index in $iDFaturacao is empty, do the Insert of $data_afaturar, but if it has a value, do the update in the id.

  • hummmmm I understood your understanding :)

  • 1

    @Andersoncarloswoss also http://sandbox.onlinephpfunctions.com/code/1382874ed041b42c070337597d182fe39c615cef

  • Thank you. It’s settled!

  • It is. You managed to see some error in the question code?

  • @Andersoncarloswoss maybe who knows Portuguese translation from Portugal to Brazilian Portuguese to better understand the question! :)

  • @Andersoncarloswoss, seeing the comments of Francisco and the edition of the question I think the corrections had already been made, so merit of Francisco

Show 4 more comments

Browser other questions tagged

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