There is a clever little thing I found in some tutorials I’d like to share^^
When I was considering adding the multiple search function for my gallery I found it’s not easy. I can’t directly write the “SELECT” sentence out because some variables might be empty. I should judge if they are empty first and then add them to the “SELECT” sentence. After writing a long string of “IF…”, I found I don’t know how to deal with one thing. I couldn’t add “AND” before the first variable but should add “AND” before others in the “SELECT” sentence. But which one is the first one keep changing. I search some tutorials online and found that they do it in a clever way, as following:
if($A1 != null){
$A2 = ” AND X=’$A1′”;}
if($B1 != null){
$B2 = ” AND Y=’$B1′”;}
$search = “SELECT * FROM table1, table2 WHERE 1=1 “;
$search .=$A2
$search .=$B2
At first I didn’t understand what 1=1 is, and suddenly I found it is just a sentence which is always true. You can also write 2=2, 80=80 or something like that. And now I can let every variable contain “AND” without worrying about if it is the first one. It’s very clever^^