PHP - Keep a variable between pages [duplicate]

0

Hello, I'm having an issue where I can not use a variable between pages

What I have to do is have a form in the page index.php, where the user can enter a tree structure of type: 1 [2 [3,5], 4 [7 [6,9,1] , 8]] - where 2 and 4 are children of 1. 3 and 5 are children of 2, and so on ...

In another page (tree.php) I identify the parents and their respective children, and store them in array_master which is an array of associative arrays (one for each parent). This part I already tested and it is working.

It turns out that this same page (tree.php) needs a form, where the user informs a node and I must say what his children are. This part is still incomplete, but just to test, when the user fills in this form I am trying to print the contents of array_master, however what is printed is:

Array ()

As if the array were empty and its contents had not been saved

Following code:

index.php:

 <body>

    <main>  
        <form action="tree.php" method="post">
            <label>Insira aqui sua estrutura de árvore</label>
            <input type="text" name="tree">
            <input type="submit" name="enviar" value="Construir!">
        </form>
    </main>

</body>

tree.php:

<?php

$array_master = array();

function get_children($string){
    $contador = 0;
    $array_filhos = array();
    for($i = 0; $i <= strlen($string); $i++){
        $char = substr($string, $i, 1);
        if($char == '['){
            $contador --;
        }
        if($char == ']'){
            $contador ++;
        }
        if($contador == 0){
            break;
        }

        if(is_numeric(substr($string, $i +1, 1)) and $contador == -1){
            //echo substr($string, $i +1, 1) . ", ";
            $filho = substr($string, $i +1, 1);
            $array_filhos[] = $filho;
        }

    }

    return $array_filhos;

}

function get_father($string){
    $pais = array();
    for($i = 0; $i <= strlen($string); $i++){
        $char = substr($string, $i, 1);
        if(is_numeric($char) && substr($string, $i +1, 1) == '['){
            $filhos = get_children(substr($string, $i +1));
            $tree_array = array("Pai" => $char,
                               "Filhos" =>$filhos);
            $pais[] = $tree_array;
        }
    }
    return $pais;
}

if(isset($_POST["tree"])){
    $tree_string = $_POST["tree"];
    $array_master = get_father($tree_string);
    print_r($array_master);// Aqui eu consigo imprimir o conteudo
}
if(isset($_POST["node"])){
    print_r($array_master); //Aqui o array é impresso vazio
}

?>

<body>

    <main>  
        <form action="tree.php" method="post">
            <label>Insira um nó para saber se ele é pai ou folha</label>
            <input type="text" name="node">
            <input type="submit" name="submit" value="Consultar!">
        </form>         
    </main>

</body>

I hope I have been clear! Thank you in advance!

Any help will be welcome:)

    
asked by anonymous 19.08.2018 / 17:53

0 answers