My array_push() on php is not working. What am i doing wrong?

05/09/2022

0

I am building a system and the objective is by a form, put the list dynamically on the screen... I tryed a lot of things and the last was using array_push() of php but always when i call the function in the site the array reset, so now i am trying put array out the function but return me a Fatal error: Uncaught TypeError: array_push(): I am beginner when is about php... Someone can help me solve that? PLS

<?php
$lista_ocorrencia = [];

function imprimir_ocorrencias($lista_ocorrencia) {

    if(isset($_GET['enviar'])){
        $n_pedido = $_GET['n-pedido'];
        $nome = $_GET['nome'];
        $cidade = $_GET['cidade'];
        $ocorrencia = $_GET['ocorrencia'];
        $valor = $_GET['valor'];

        $ocorrencia = "<ul class='modelo'>
                        <li class='w10'>$n_pedido</li>
                        <li class='w20'>$nome</li>
                        <li class='w15'>$cidade</li>
                        <li class='w40'>$ocorrencia</li>
                        <li class='w15'>$valor</li>
                    </ul>";
                    
        array_push($lista_ocorrencia, $ocorrencia);

        foreach($lista_ocorrencia as $x){
            echo $x;
        }

    }
}
imprimir_ocorrencias($lista_ocorrencia);
?>
Samuel Matos

Samuel Matos

Responder

Posts

12/03/2023

Reginaldo Assunção

Oi. The reason why the array resets every time the function is called is that the array is re-initialized every time the page is loaded. To solve this, you can use sessions to store the array and maintain its values across different requests.

You can modify your code as follows:

php
Copy code
<?php
session_start(); // Start the session

function imprimir_ocorrencias() {

    $lista_ocorrencia = isset($_SESSION['lista_ocorrencia']) ? $_SESSION['lista_ocorrencia'] : array(); // Retrieve the array from the session or create an empty array if it doesn't exist

    if(isset($_GET['enviar'])){
        $n_pedido = $_GET['n-pedido'];
        $nome = $_GET['nome'];
        $cidade = $_GET['cidade'];
        $ocorrencia = $_GET['ocorrencia'];
        $valor = $_GET['valor'];

        $ocorrencia = "<ul class='modelo'>
                        <li class='w10'>$n_pedido</li>
                        <li class='w20'>$nome</li>
                        <li class='w15'>$cidade</li>
                        <li class='w40'>$ocorrencia</li>
                        <li class='w15'>$valor</li>
                    </ul>";

        array_push($lista_ocorrencia, $ocorrencia);
        $_SESSION['lista_ocorrencia'] = $lista_ocorrencia; // Store the updated array in the session

        foreach($lista_ocorrencia as $x){
            echo $x;
        }

    }
}

imprimir_ocorrencias();
?>

With this modification, the lista_ocorrencia array is retrieved from the session at the beginning of the function, and any updates to the array are stored back into the session. This ensures that the array maintains its values across different requests.





Regenerate response
Responder

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar