Get variable on another page

3

After searching the data in mysql with php and displaying them on the page, I would like to know how to get the same results from this page where it is displayed and display them on another page.

<?php  

//Cria a variavel telefone enviada do formulario na pagina painel.php
$telefone = $_POST['telefone'];

//Verifica se o valor de telefone não é vazio.
if (empty($telefone)) { 
 header("location: painel.php");
 exit;
}
//Conecta com o banco e seleciona os arquivos para efetuar a contagem.
$con = mysqli_connect("localhost", "root", "", "pizzaria");
$query = mysqli_query($con, "SELECT nome, numero, endereco from clientes where telefone = $telefone");
$count = mysqli_num_rows($query);

//Se a contagem for 0
if ($count == 0) { ?>
<div class="alert alert-danger" role="alert">
    <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
    Ops! não encontramos nenhum cliente com este telefone,
    <a href="painel.php" class="alert-link">tente novamente!</a>
</div>
<?php
die;
//Se a contagem for 1
} else {
if ($count == 1) { ?>
    <div class="alert alert-success">
        Encontramos 1 cliente com este telefone!
    </div>
    <?php
}
}
//Mostra todos os resultados encontrados.
while ($resultado = mysqli_fetch_assoc($query)) {
echo "Cliente: ". $resultado['nome']. "<br/>";
echo "Endereço: ".$resultado['endereco']. "<br/>";
echo "N°: ".$resultado['numero']. "<br/>";
}

?>

I would like to get the results of array $ result ['name'], $ result ['address'] and $ result ['number'] and throw them on the "new-request.php" page and then use them to insert into another table in the bank.

    
asked by anonymous 05.03.2015 / 04:25

1 answer

2

You can use session 's PHP. It is very easy to use and only needs to be started at the beginning of the script and then accessed through the global variable $_SESSION .

On the page that runs the query:

// no início do arquivo
session_start();

// após executar a query
$_SESSION["clientes"] = array();
while ($resultado = mysqli_fetch_assoc($query)) {
    $_SESSION["clientes"][] = $resultado;
}

On another page:

// no inicio do arquivo
session_start();

while ($_SESSION["clientes"] as $cliente) {
    var_dump($cliente);
}

Important: The session_start function must be executed only once and before executing any command that displays information for the user, such as echo , print , ie recommended to put well at the beginning of the file.

Note: I do not recommend passing the results of a full survey via $_SESSION . An alternative would be to save the filters or even the query in the session and then run again on the other page.

    
05.03.2015 / 14:58