CONSULTATION JOINING VARIOUS COLUMNS

1

I have a table with 5 columns ...

I search the database through the following line where $ search contains the search criteria that I want to make.

$sql=("SELECT * FROM bdfo WHERE piso LIKE '%$buscar%' OR sala LIKE 
      '%$buscar%' OR fila LIKE '%$buscar%' OR posicion_de_fila LIKE 
      '%$buscar%' OR usuario LIKE '%$buscar%'" );

If in my input variable "03", it shows me exactly the rows that contain this record, in the same way for each of the columns.

So far everything is going well, what I need when I enter the variable "03 T 102A ..." shows me the same results to decrease the records in the table.

Thank you!

What happens is the following .. When I perform a search with the following line, I get the following result.

$ sql = ("SELECT * FROM bdfo WHERE floor LIKE '% $ search%' OR room LIKE '% $ search%' OR row LIKE '% $ search%' OR position_of_yellow LIKE '% $ search%' OR user LIKE '% $ search%' ");

What I want is that when performing the next more specific search show me results

    
asked by Joel Alexis Ramirez Figueroa 20.12.2018 в 02:04
source

3 answers

0

Joel, according to what I interpret, you should do a dynamic query, in which for each different word that is included in the search criteria, add a condition in your SQL query.

That is, when you retrieve what the user has entered in the search, by means of the split function (you can see a reference here )

You will generate an array with the different terms entered.

Once with this, you should go through this array and for each term add the condition to your SQL statement.

As an example, I indicate if your array had 3 search terms, the sentence should look like this:

$sql=("SELECT * FROM bdfo WHERE 

(floor LIKE '% $ search1%' OR room LIKE '% $ search1%' OR row LIKE '% $ search1%' OR position_of_yellow LIKE '% $ search1%' OR user LIKE '% $ search1%') OR (floor LIKE '% $ search2%' OR room LIKE '% $ search2%' OR row LIKE '% $ search2%' OR position_of_yellow LIKE '% $ search2%' OR user LIKE '% $ search2%') OR (floor LIKE '% $ search3%' OR room LIKE '% $ search3%' OR row LIKE '% $ search3%' OR position_of_file LIKE '% $ search3%' OR user LIKE '% $ search3%') ");

Where "$ search1", "$ search2", "$ search3" ... are the different terms that the user has entered.

I hope it serves you.

    
answered by 21.12.2018 в 07:58
0

You take the text of the box, you extract it in the words entries (a split). Then you make a cycle (for) and for each term you generate the same query, concatenating the results in an array that you have outside the cycle.

    
answered by 21.12.2018 в 09:10
0

I think it would be much easier for you to get ALL the data from the table in case it is not a very large table and use the DataTables plugin link .

On the other hand If you want to make a query every time you look for something, then it is to separate search strings by spaces with a split.

    
answered by 21.12.2018 в 09:22