Variable value in PHP

Asked

Viewed 71 times

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 $25Item to get 25Item coupled with the counter as if it were assigning:

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

And so on. I 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 <= $nCtd ; $i++){

$item['a25Item'. $i] = $_GET['a25Item'. $i]; $Prod['a25Prod'. $i] = $_GET['a25Prod'. $i]; $Quan['a25Quan'. $i] = $_GET['a25Quan'. $i]; $desc['a25Desc'. $i] = $_GET['a25Desc'. $i]; $Prec['a25Prec'. $i] = $_GET['a25Prec'. $i]; $subt['a25Subt'. $i] = $_GET['a25Subt'. $i];

}

And to get the values:

for($i = 0; $i <= $qtdItem -1 ; $i++){

$item['a25Item'. $i]. $Prod['a25Prod'. $i]. $Quan['a25Quan'. $i]. $desc['a25Desc'. $i]. $Prec['a25Prec'. $i]. $subt['a25Subt'. $i]; }

That way I can ride the way I need to.Thanks! SOLVED!

2 answers

2


Use a 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.

2

It is not possible to create variables by defining their name as if it were a string and the concatenation of string in PHP it’s not like this:

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

One way that might be useful to you and I believe it’s better, is by 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’d be saving the data in a array where the key would be the value of i. And for every position in array, $arr[i], you would have another array with the information you need, such as: a25Item, a25Prod and a25Desc.

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

In relation to the concatenation of string you do it this way:

$_GET['a25Item' . i]

Browser other questions tagged

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