Skip to content
Advertisement

Search function when one field is blank

I need to know if there’s a more efficient way to do a search when a user leaves a blank field

JavaScript

My question is how do I do this? My own conclusion was to use a variety of

JavaScript

and so on then corresponding each with its own SELECT * FROM.... but this seems so bothersome and not that dynamic especially if I decided to add a third or fourth field and it’ll be a whole new additional set of ifs. Is there a more efficient way to do this?

Perhaps in MySQL?

Advertisement

Answer

Like I said in the comments section, I don’t know if this is more efficient, but I think it’s easier to read and will help ease the pain of changing all conditions of your statement. I did not find tutorials about things like that, but here’s a modified example of something I’ve done in the past for a real estate company:

JavaScript

Please note, for search engines, you might want to use

JavaScript

or something similar as your SQL query, but my example required exact matches.

This has a few requirement tweaks, your conditionals will definitely be different than these, but I just want to share the idea. You see, all you have to do if you want to add more options (columns) is add a set of if‘s that match your requirement and then accordingly add the variable to the final string. Hope this helps at least a little.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement