Rename posted fields into PHP variables

Asked

Viewed 362 times

0

I have a form with dynamic fields. with inputs with Names like this:

<input type="hidden" name="nome_prod1" value="Shampoo Hidratante Dazen Elegance">
<input data-theme="b" value="0" name="qtd1" type="text" id="1"/>

<input type="hidden" name="nome_prod2" value="Shampoo Hidratante Dazen Elegance">
<input data-theme="b" value="0" name="qtd2" type="text" id="1"/>

each one with its value, the problem is at the time of showing this because did not want to show so little save in the bank the fields that have quantity 0.

I used the command:

foreach( $_POST as $nome_campo => $valor)
{ 
   $comando = "$" . $nome_campo . "='" . $valor . "';"; 
   eval($comando); 
}

However he brought all the inputs, I tried to treat not to show the ones that have qtd = 0 but the problem is in finding the variable I put a counter $i within foreach, then the variable name qtd I tried to call it that:

if ($qtd{$i} == 0 ) {}

But it didn’t work, as I do it fetching a dynamic variable?

$qtd1
$qtd2
$qtd3

1 answer

3


First of all, answering what was asked, a path would be to use "variables":

foreach( $_POST as $nome_campo => $valor) { 
   $$nome_campo = $valor;
}

But this is far from a good idea. Any misuse of your PHP (for example, the client sending arbitrary data) will overwrite application variables.

As well remembered by Papa Charlie, PHP even has something ready that already does it automatically, the extract().

Like the above solutions (be the extract or the loop) has its inherent problems, a more elegant solution would be to use this structure:

<input type="hidden" name="nome_prod[]" value="Shampoo Hidratante Dazen Elegance">
<input data-theme="b" value="0" name="qtd[]" type="text" id="1"/>

And in PHP something like that:

$nome  = $_POST['nome_prod'];
$qtd   = $_POST['qtd'];

// aqui é só usar os dados como quiser, já estão indexados desde a captura.
$count = count( $qtd );
for( $i = 0; $i < $count; ++$i ) {
   echo 'Produto: '.$nome[$i].' - Quantidade: '.$qtd[$i]."<br>\n";
}

In a complete application, it is necessary to sanitize the data before use, obviously (regardless of the approach used).

As for not using zeroed values, just something like this inside the loop:

if( 0 + $qtd[$i] ) {
    // usa o valor como quiser
}

the code within the block of if will only be executed with values other than 0. The trick used is to force a cast with 0 + to treat the value as numerical (there are several ways to do this, this is one of them. see how to do the Casts and conversions in the PHP manual, in the variable part).

  • I do not know if I understood the question well, but it would not be the case to use extract?

  • 1

    @Papacharlie would be if it was to encourage his first idea, which I condemn. It’s that in Extract you can’t give it for an IF as I mentioned above, either take everything or take nothing. Eval, Extract, $_REQUEST, $$variables and everything that is very "generic" in PHP ends up being a big problem. But if you post an example with Extract, I think it’s a valid alternative (which I don’t recommend, but wouldn’t be wrong). I just suggest making it clear that you suffer from the same problems I mentioned above. Anyway, it is a valid comment.

  • I know, I agree 1000%, and I wouldn’t dare answer using extract. The ideal would be to recommend a more correct approach as you did. I was the one who didn’t understand the question, I wanted to know if what I needed was in line with extract.

  • 1

    @Papacharlie in this case, I put a mention to your comment in the reply, at least it is not missing to warn of the option.

Browser other questions tagged

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