Foreach inside foreach with parent elements, children and grandchildren

Asked

Viewed 200 times

0

Good afternoon gentlemen, I’m trying to print a paragraph dynamically but I’m having some difficulty.

I have the following html code:

    form {} label {
      display: block
    }
    span {
      display: block;
      float: left;
      width: 40px;
    }
    ul li {
      list-style: none;
    }
<form name="geraTabela" action="RecebeForm.php" method="POST">
  <ul>
    <li>
      <input type="hidden" name="id_texto[]" value="1">
      <label> <span>Pai</span>
        <input type="text" name="Pai[]">
      </label>
      <label> <span>Filho</span>
        <input type="text" name="filho1[]">
      </label>
      <label> <span>Neto</span>
        <input type="text" name="neto[]">
      </label>
    </li>
    <br>
    <li>
      <input type="hidden" name="id_texto[]" value="2">
      <label> <span>Pai</span>
        <input type="text" name="pai[]">
      </label>
      <label> <span>Filho</span>
        <input type="text" name="filho2[]">
      </label>
      <label> <span>Neto</span>
        <input type="text" name="neto[]">
      </label>
    </li>
    <br>
    <li>
      <input type="hidden" name="id_texto[]" value="3">
      <label> <span>Pai</span>
        <input type="text" name="pai[]">
      </label>
      <label> <span>Filho</span>
        <input type="text" name="filho3[]">
      </label>
      <label> <span>Neto</span>
        <input type="text" name="neto[]">
      </label>
    </li>
  </ul>
  <!---->
  <button type="submit" name="btn">Enviar</button>
</form>

I would like the code printed as follows:

Pai 1
 Filho 1 de Pai 1
 neto 1 de Filho 1
 Filho 2 de Pai 1
 Neto 1 de Filho 2
 Neto 2 de Filho 2

Pai 2
 Filho 1 de Pai 2
 Filho 2 de Pai 2
 Neto 1 de Filho 2

I managed to 'tie' the Father with the Sons through the ID. I am having a hard time 'tying' grandchildren with their children would like just a light on how to accomplish this task. I thank you already!

<?Php
if(isset($_POST['id_texto'])){
$id_texto = $_POST['id_texto'];
    foreach($id_texto as $id){
        if(isset($_POST['pai'])){
        $pai = $_POST['pai'];
            foreach($pai as $pais){
                if(isset($_POST["filho'.$id.'"])){
                $paragrafo = $_POST["filho'.$id.'"];
                    foreach($filho as $filhos){
                        if(isset($_POST["neto"])){
                        $neto = $_POST["neto"];
                            foreach($neto as $netos){

                            }
                        }
                    }
                }
            }
        }
    }
}
?>
  • 2

    And php where you enter?

  • The form is correct and you just want to handle the POST printing? Because for me the form is a little strange.You cite 4 grandchildren but in the form there are only 3, cite 2 parents but in the form there are 3...if you can be clearer in your intentions would be better.

  • Hello Carla, thank you for answering, I used only one example, since in fact it is all dynamic, my intention was to show that there can be several grandchildren, several children. Thank you!

No answers

Browser other questions tagged

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