How to properly make this condition?

Asked

Viewed 189 times

2

I made this condition, so that if the person searches by name Andrey will run a require, otherwise it will not display, so any name I put in the search field is bringing the require, so is 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 inserir a descrição da imagem aqui

  • See that in the code you compare $_POST['nome'] This seems not to exist the correct would be $_POST['palavra'] nos if. Need to clarify this.

  • I put 'word' and still not bringing the require... my ours getting crazy with it already

  • I edited the answer with the correction.

  • Dude, I don’t get it, the require only works if I put it on LSE, but whatever I look for ends up displaying it

  • Before you were comparing the wrong values there was no field called nome in your form (html) there $_POST['nome'] == 'algo' will always return false. You need to define what you want to compare.

  • yes, but I switched for word and solved nothing

  • There must be a problem elsewhere in the code, because the comparison is what is in the answer. Need to do a thorough debug now, print the value of the variables check where the code passes.

Show 2 more comments

3 answers

8

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

It does not work as expected. The comparison made is if there is something in the string compares with true. It is common in programming languages you repeat the predicate for various comparisons, could rewrite 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 various values is the function in_array().

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

If you find more readable you can take the result of the functions and assign in variables and compare in 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';
}

2


Treatment of String

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

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

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

Applying to the two examples of the first 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(); replaces the accented characters
  • strtolower(); converts all string characters to lower case
  • Trim(); excludes all whitespace at the beginning and end of the string
  • 2

    Buddy, thanks, it worked out the way you showed it

  • But there is a problem @Filipe Ricardo, if I put for example to search 'Andrêy' returns everything blank... how to solve this?

  • The way I went is to make the condition, this query you are doing by the database? I suggest you post the code of that part. With the code I gave you, Andrêy is Andrey, follow the link with the example running https://repl.it/MMxC/6

  • see here the code: if (isset($_POST['word'])) { // remove accents $word = preg_replace("/&([a-z])[a-z]+;/i", "$1", htmlentities(strtolower(Trim($_POST['word']))); } if (!Empty($word) && in_array($palavra, array('andrey ferraz', 'andrey', 'ferraz', 'Andrêy', 'Andrêy Ferraz', 'Andrêy Martins Ferraz'))){&#xA; require 'andreyferraz.php';&#xA; }&#xA;E sim, é pelo banco de dados mysql

  • As you have converted everything the user typed leaving without accent and in lowercase, in condition you need to put everything without accent and in lowercase as well. Follow this example by working https://repl.it/MMxC/7

  • It is more and I did it there, and when I put in the search field the word "Andrêy" does not return me anything, but if I put "Andrey" there returns

  • does so, put a print_r($_POST['word']); in a line above your condition, repeat the search by typing "Andrêy" and paste here the result of this print

  • I did and returned nothing!

  • If the print did not return anything then the problem is in $results, your sql query did not return any results. Post your code in the part where the query is made.

Show 5 more comments

1

Now the code is like this, and this time the require is no longer displayed in any way, and it should be displayed when you search by 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.";
}

Browser other questions tagged

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