How to generate sequential variables (ex: d1, d2, d3) and then merge all into a single PHP variable

-1

I have a script using foreach that generates the variables $ d, $ m, which get the value of a mysql query and put this value in the input

echo "
<input type='text' name='tipo' value='".$d."'>
<input type='text' name='metragem' value='".$m."'>
";

Then I wanted each field to be unique, because the query returns several rows, so I added $ n

$n = 0;
foreach($unidades as $unidade){
    $n++
    echo "
        <input type='text' name='tipo".$n."' value='".$d."'>
        <input type='text' name='metragem' value='".$m."'>
    ";
}

So far so good, but I need now to put all of them together in a single $ valued variable and $ mtotal, eg:

$mtotal = 'tipo'.$n1.'|'.'tipo'.$n.'|'.'tipo'.$n(quantos n tiverem);
    
asked by anonymous 27.07.2018 / 13:41

1 answer

1

You can solve your problem by creating $mtotal and $dtotal within your own foreach, like this:

$n = 0;
$mtotal = ''; // inicializa a variável mtotal
foreach($unidades as $unidade){
    $n++
    echo "
        <input type='text' name='tipo".$n."' value='".$d."'>
        <input type='text' name='metragem' value='".$m."'>
    ";

    //concatena a variável existente com o formato desejado
    $mtotal .= 'tipo'.$n.'|';

}

Doing the same thing for $dtotal , you did not give the example of how it would look.

    
27.07.2018 / 13:46