how to create a search engine that can consult different columns? [closed]

1

I have a question for example I have a

<input type="text" id="busqueda">

and in my database I have the columns id, name, surname, mail, telephone

Could with a single input perform a search? for example if I write a name for example pedro, look for me in the name column, or if I write an email, I can validate if the input contains a @ or if it is a telephone number to be able to validate if what is entered in the input are numbers , something like that

    
asked by Juan Jose 27.12.2018 в 05:01
source

2 answers

0

You can use the substr_count feature of PHP to identify if it's a email when searching for the arroba.

if (substr_count($busqueda, '@') == 1) {
   $Query = "SELECT * FROM tabla WHERE correo = '$busqueda'";
} else {
   $Query = "SELECT * FROM tabla WHERE 
   id LIKE '%$busqueda%' OR 
   nombre LIKE '%$busqueda%' OR
   apellido LIKE '%$busqueda%' OR
   telefono LIKE '%$busqueda%'";
}

$Result = DB::Connect($Query);
  

It's just an example conditioning the mail search by identifying   the arroba, you can do the same with the other fields, how   Identify if the data to be searched is the whole type, if so, we could   know what the id or telephone is according to its length, so   you could do with each field or add LIKE for each column.

    
answered by 27.12.2018 / 15:22
source
0

You could use regular expressions to see what kind of data you have in your input. Suppose you want to validate for 3 types of data: name (string) phone number (numeric) or email. For those 3 you could do something like this:

Assuming that in the variable $ input you have the value of your input

 if (/^([0-9])*$/.test($input)){
   funcionBuscarPorNumero($input) //llamas a una funcion que busque por numero
 } else if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+).\w+$/.test($input)) {
   funcionBuscarPorEmail($input) //llamas a funcion que busque por email
 } else {
   funcionBuscarPorNombre($input)} //llamas a funcion que busque por nombre
    
answered by 27.12.2018 в 14:31