How to return a mysql query as a link?

Asked

Viewed 276 times

1

Folks I am developing an application, and I would like when a search for the search form is conducted to return a value as a link to a given page associated with the searched name, someone knows how to tell me how to do this?

Here is the part of the code that brings the results:

$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)) {
        echo utf8_encode("<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.";
}
  • Edit the question and place the part of the code that is returning the result.

  • in the company table you have a column with the website address ?

2 answers

4

check if in your table enterprise has the spine website / url, if not, create and place this line inside the while:

echo "<strong>site: </strong>". $linha['site']."</br>";

Thus remaining the code:

$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)) {
        echo utf8_encode("<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>";
        // Mostra o link para site
        echo '<strong>site: </strong><a href="'.$linha['site'].'">'.$linha['site'].'</a></br>';
        echo "<hr>";
    }
} else {
    echo "Nenhum resultado para a sua busca.";
}
  • beauty in the table I’ll add the field to insert the link, I hadn’t thought of it that way, thank you very much!

  • there is a problem, this way then all the links will be set with the same value

  • No return with the same value.

-1


To solve I used a condition, so everything worked out fine! For each registered name to using this condition:

 if (isset($_POST['nome']) === 'Empresa Cadastrada' || 'Empresa' || 'Cadastrada'){
            echo '<strong>Entre em contato por aqui: </strong><a href="empresa.php"'.$linha['link'].'>'.$linha['link'].'</a></br>';
        } else{
            echo "Empresa não associada";
        }
  • Can you explain it better? So I think your answer won’t help much if someone has the same problem.

  • 1

    edited, look at it !

  • Thanks, improved

Browser other questions tagged

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