How to correctly do this condition?

2

I made this condition so that if the person searches for the name andrey will execute a require, otherwise it will not display, so any name that I put in the search field is bringing the require, so here's my code:

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>";
        if (isset($_POST['nome']) === 'Andrêy Ferraz' || 'Andrêy' || 'Ferraz' || 'Andrey'){
           require 'andreyferraz.php';
        }
    }
} else {
    echo "Nenhum resultado para a sua busca.";
}

$conexao->close();

@rray gave it here in the search result

    
asked by anonymous 09.10.2017 / 19:54

3 answers

2
  

String handling

Another important thing is to treat this string, we can never trust what the user is going to type, and to do this condition without accentuation with everything tiny will increase the search success rate.

if (isset($_POST['palavra'])) {
  $palavra = preg_replace("/&([a-z])[a-z]+;/i", "$1", htmlentities(strtolower(trim($_POST['palavra'])))); 
}

After that you use the $palavra variable to make the condition instead of $_POST['palavra'] .

Applying in the two examples of the first response of rray :

if (isset($palavra) &&
   ($palavra === 'andrey ferraz' ||
    $palavra === 'andrey' ||
    $palavra === 'ferraz')){

.

if (!empty($palavra) && 
    in_array($palavra, array('andrey ferraz', 'andrey', 'ferraz'))){

Understanding what each function does:

  • preg_replace (); overrides accented characters
  • strtolower (); converts all string characters to lowercase
  • trim (); exclude all whitespace at the beginning and end of the string
10.10.2017 / 18:34
8
if (isset($_POST['palavra']) === 'Andrêy Ferraz' || 'Andrêy' || 'Ferraz' || 'Andrey'){

Does not work as expected. The comparison made is if something exists in the string compares with true . It is common in programming languages that you repeat the predicate for several comparisons, you could rewrite it as follows:

if (isset($_POST['palavra']) &&
   ($_POST['palavra'] === 'Andrêy Ferraz' ||
    $_POST['palavra'] === 'Andrêy' ||
    $_POST['palavra'] === 'Ferraz' ||
    $_POST['palavra'] === 'Andrey')){

In this case the best option to compare multiple values is the in_array() function.

if (!empty($_POST['palavra']) && 
    in_array($_POST['palavra'], array('Andrêy Ferraz', 'Andrêy', 'Ferraz', 'Andrey'))){

If you find it more readable you can get the result of the functions and assign them in variables and compare them in the if.

$temNome = !empty($_POST['palavra']) ? $_POST['palavra'] : false;
$nomeEncontrado = in_array($temNome, array('Andrêy Ferraz', 'Andrêy', 'Ferraz', 'Andrey'));

if($temNome && $nomeEncontrado){
   require 'arquivo.php';
}
    
09.10.2017 / 20:02
1

Now the code is like this, and this time the require is no longer being displayed in any way, and it should be displayed when searching for the name 'andrey':

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>";
        if (!empty($_POST['nome']) &&
            in_array($_POST['nome'], array('Andrêy Ferraz', 'Andrêy', 'Ferraz', 'Andrey'))){

         print  require 'andreyferraz.php';
        }
    }
} else {
    echo "Nenhum resultado para a sua busca.";
}
    
09.10.2017 / 20:13