Variable value in PHP

1

Hello

I get the variables like this:

&a25Item1=01&a25Prod1=00123&a25Desc1=ProdutoAcabadoUm

The number will depend on $qtdProd

&a25Item2=01&a25Prod2=00123&a25Desc2=ProdutoAcabadoDois

As in PHP I do the assignment below:

for($i = 0; $i <= $qtdProd ; $i++){

    $a25Item i = $_GET['a25Item' i ];
    $a25Prod i = $_GET['a25Prod' i ];
    $a25Desc i = $_GET['a25Desc' i ];
}

I need $ a25Item to receive the 25Item by joining with the counter as if assigning:

$a25Item1 = $_GET['a25Item1'];
$a25Item2 = $_GET['a25Item2'];

And so on. Thank you in advance!

Thank you all! It worked like this:

$ item = array (); $ prod = array (); $ quan = array (); $ desc = array (); $ prec = array (); $ subt = array ();

for ($ i = 0; $ i

asked by anonymous 09.08.2018 / 14:49

2 answers

2

Use array to store items, so you can do this:

$itens = array();
$produtos = array();
$descricoes = array();
for($i = 0; $i <= $qtdProd ; $i++){
  $itens['a25Item'.i]=$_GET['a25Item'.i];
  $produtos['a25Prod'.i]=$_GET['a25Prod'.i];
  $descricoes['a25Desc'.i]=$_GET['a25Desc'.i];
}

The key to access the value will be for example: $itens['a25Item0'], $itens['a25Item1'] , this will return the values passed by $_GET['a25Item0'], $_GET['a25Item1']

To access the items:

foreach($itens as $item => $chave){
  echo $item;
  echo $chave;
}
foreach($produtos as $produto => $chave){
  echo $produto;
  echo $chave;
}
foreach($descricoes as $descricao=> $chave){
  echo $descricao;
  echo $chave;
}

Remembering that there may be other better options.

    
09.08.2018 / 15:05
2

It is not possible to create variables by setting their name to be a string and the concatenation of string to PHP is not this way:

for($i = 0; $i <= $qtdProd ; $i++){
    $a25Item i = $_GET['a25Item' + i ];
    $a25Prod i = $_GET['a25Prod' + i ];
    $a25Desc i = $_GET['a25Desc' + i ];
}

A form that may be useful to you and I believe that is better, is doing so:

$arr = [];
for($i = 0; $i <= $qtdProd ; $i++){
    $arr[i]['a25Item'] = $_GET['a25Item' . i];
    $arr[i]['a25Prod'] = $_GET['a25Prod' . i];
    $arr[i]['a25Desc'] = $_GET['a25Desc' . i];
}

You would be saving the data to a array where key would be the value of i . And for each position in array , $arr[i] , you would have another array with the information you need, such as a25Item , a25Prod and a25Desc .

OBS: I think it would be best to name them only as: item , prod and desc .

Regarding the concatenation of string you do it as follows:

$_GET['a25Item' . i]
    
09.08.2018 / 15:23