Print Recursive Table - Idparent

1

I have the following BD:

and what I want is to do a questionnaire in the following way:

Question 1

  • Answer 1
  • Answer 2
  • Answer 3
  • Answer 4
  • Question 2

  • Answer 1
  • Answer 2
  • Answer 3
  • Answer 4
  • I'm trying with the following code:

    foreach ($data1 as $key)
    {
    
    if($key->idparent == 0 && $key->idevaluacion == 3)
    {
        echo "<h3>".$key->pregunta."</h3><br>";
        foreach ($data1 as $key => $value) {
                if($value->idparent == 1 && $value->idevaluacion == 3){
                    echo $value->pregunta."<br>";
                }
            }   
    }
    
    }   
    

    But I need that $value->idparent == 1 (this one) is printed in a loop with the parents ID and I do not know how to do it.

        
    asked by Luis Vega 30.08.2016 в 00:34
    source

    1 answer

    0

    After 3 days I solved it, if someone needs it there, although it needs to be optimized. but it works anyway.

    <?php 
    $padre = array();
    $preguntaP = array();
    foreach ($data1 as $key =>$value) 
    {
        if($value->idparent == 0)
        {
            array_push($padre,$value->idpreguntaeval);        
            array_push($preguntaP, $value->pregunta);
        }
    }
    for ($i=0; $i < count($padre) ; $i++) 
    {   
        for ($i=0; $i < count($preguntaP); $i++) 
        { 
            echo $preguntaP[$i]."<br>";
            foreach ($data1 as $key ) 
            {           
                if ($key->idparent == $padre[$i]) 
                {
                    echo $key->pregunta."<br>";
                }
            }
        }
    }
    ?>
    

    Greetings.

        
    answered by 02.09.2016 в 05:55