How do you get the result published in the right place?

Asked

Viewed 28 times

0

I’m using the require on this page of mine, so the result is being printed after the footer, someone would tell me how to solve, follow the code:

<?php

require ("principal.php");

$servidor = "localhost";
$usuario = "root";
$senha = "root";
$tabela = "guianortecapixaba";

$conexao = new mysqli($servidor, $usuario, $senha, $tabela);

if ($conexao->connect_error) {
    die("Erro: " . $conexao->connect_error);
}

$busca = $_POST['palavra'];
$cidade = $_POST ['cidade'];

$sql = "SELECT * FROM empresa WHERE nome LIKE '%$busca%' AND cidade = '$cidade'";
$resultados = $conexao->query($sql);


if ($resultados->num_rows > 0) {
    while($linha = mysqli_fetch_array($resultados)) {
        print("<strong>Nome: </strong>" . $linha['nome'] . "</br>");
        print ("<strong>Endereço: </strong>" . $linha['endereco']."</br>");
        if( isset($_POST['cidade']) && $_POST['cidade'] === 'sao-gabriel-da-palha' ) {
            $fromPerson = 'São Gabriel da Palha';
            echo "<strong>Cidade: </strong>".$fromPerson."</br>";
        }
        print ("<strong>Telefone: </strong>" . $linha['telefone']."</br>");
        echo "<strong>email: </strong>". $linha['email']."</br>";
        echo "<hr>";
    }
} else {
    echo "Nenhum resultado para a sua busca.";
}

$conexao->close();

?>
  • It’s quite simple your footer is inside the main.php I suppose .... and the execution is being done later, interesting would be to take this php block to a separate file and make include where it should bring the result

  • Give an example there because I could not, I’m still learning so such difficulties

  • Edit the question and add the footer part where the information should appear so I can help you

  • It is not in the footer that the information is to appear, but in the main column of the page, so the information is being printed after the footer

  • Answered see if it helps you

1 answer

1


So come on, create a new file with the contents by removing the contents from the current file:

Let’s hypothetically call the file php return. (name should be changed to your need)

$servidor = "localhost";
$usuario = "root";
$senha = "root";
$tabela = "guianortecapixaba";

$conexao = new mysqli($servidor, $usuario, $senha, $tabela);

if ($conexao->connect_error) {
    die("Erro: " . $conexao->connect_error);
}

$busca = $_POST['palavra'];
$cidade = $_POST ['cidade'];

$sql = "SELECT * FROM empresa WHERE nome LIKE '%$busca%' AND cidade = '$cidade'";
$resultados = $conexao->query($sql);


if ($resultados->num_rows > 0) {
    while($linha = mysqli_fetch_array($resultados)) {
        print("<strong>Nome: </strong>" . $linha['nome'] . "</br>");
        print ("<strong>Endereço: </strong>" . $linha['endereco']."</br>");
        if( isset($_POST['cidade']) && $_POST['cidade'] === 'sao-gabriel-da-palha' ) {
            $fromPerson = 'São Gabriel da Palha';
            echo "<strong>Cidade: </strong>".$fromPerson."</br>";
        }
        print ("<strong>Telefone: </strong>" . $linha['telefone']."</br>");
        echo "<strong>email: </strong>". $linha['email']."</br>";
        echo "<hr>";
    }
} else {
    echo "Nenhum resultado para a sua busca.";
}

After this inside the main.php exactly at the point where to return this data you will insert:

<?php
    require ("retorno.php");

Remembering that php return. is the name we gave to this new file, I believe this solves your problem.

Browser other questions tagged

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