Join variables in PHP

0

Hello, I have a form that captures some data, the variables that capture this data are:

$parentesco1 = $_POST['parentesco1'];
$edad1 = $_POST['edad1'];
$NivelAcademico1 = $_POST['NivelAcademico1'];
$ocupacion1 = $_POST['ocupacion1'];
$salario1 = $_POST['salario1'];

But it is a table so the variables are 6 times more alone than with their numbers increased

$parentesco2 = $_POST['parentesco2'];
$edad2 = $_POST['edad2'];
$NivelAcademico2 = $_POST['NivelAcademico2'];
$ocupacion2 = $_POST['ocupacion2'];
$salario2 = $_POST['salario2'];

And so until I get to 6, my question is, how can I print this data in a for using the variable of for as a counter in the variable, that is to say that the number of the variable increases based on the variable of the FOR

for ($i=1; $i <=6 ; $i++) { 
    $imprimir = "$parentesco"."$i";
    echo "$imprimir";
};

This is what I tried to do but I do not know how I could do it

var_dump($_POST) give me this:

array (size=29)
  'parentesco1' => string 'padre' (length=5)
  'edad1' => string '35' (length=2)
  'NivelAcademico1' => string 'bachillerato' (length=12)
  'ocupacion1' => string 'Vendedor' (length=8)
  'salario1' => string '50' (length=2)
  'parentesco2' => string 'madre' (length=5)
  'edad2' => string '58' (length=2)
  'anio2' => string 'Comerciante' (length=11)
  'salario2' => string '150' (length=3)
  'parentesco3' => string 'hermana' (length=7)
  'edad3' => string '18' (length=2)
  'NivelAcademico3' => string 'bachillerato' (length=12)
  'anio3' => string 'Maquilera' (length=9)
  'salario3' => string '200' (length=3)
  'parentesco4' => string 'tio' (length=3)
  'NivelAcademico4' => string 'bachillerato' (length=12)
  'anio4' => string 'Vendedora' (length=9)
  'salario4' => string '350' (length=3)
  'parentesco5' => string 'abuela' (length=6)
  'edad5' => string '52' (length=2)
  'NivelAcademico5' => string 'basico' (length=6)
  'anio5' => string 'NO' (length=2)
  'salario5' => string '0' (length=1)
  'parentesco6' => string 'hermano' (length=7)
  'edad6' => string '18' (length=2)
  'NivelAcademico6' => string 'no' (length=2)
  'anio6' => string 'NO' (length=2)
  'salario6' => string '0' (length=1)
  'form10' => string '' (length=0)
    
asked by DMya 18.07.2018 в 04:21
source

1 answer

0

For future developments I recommend using the double brackets in the name of the fields, for example:

<input type="text" name="nombre[]"  value="Andres" />
<input type="text" name="nombre[]"  value="Paco" />

This way it is much easier to extract the information, and in this way you will have a cleaner code:

var_dump($_POST['nombre']); 
/*array(2) {   [0]=>   string(6) "Andres"   [1]=>   string(4) "Paco" }*/

Answering your question:

First you need to define the names of the fields that are recurrent:

$campos = array("parentesco","edad","NivelAcademico","ocupacion","salario");

In the next step we need two loops, the first one to run 6 times, and the second one that traverses the variable $ fields, which will run within the first one

    for ($i=1; $i <=6 ; $i++) { 
        foreach($campos as $campo) {
            $nombre_campo = $campo . $i;
            //Se comprueba que el campo exista en la variable $_POST
            if(!array_key_exists($nombre_campo, $_POST)) continue;
            echo $nombre_campo . "; Valor: " . $_POST[$nombre_campo];
        }
    }
    
answered by 18.07.2018 в 05:58