networkthis Posted November 12, 2008 Share Posted November 12, 2008 Heres what I am trying to do... I am trying to simplify my script. I have several search options on my page. I am trying to get the variables if passed and add them into a mysql query...right now I have many single and multiple queries... example if a user selects only one field it will search only that field, if they search two fields it will search them both, three fields it will search all three etc...... and display the results.... here is what I have so far <?php if ($city) { $data_p = mysql_query("SELECT * FROM list WHERE city LIKE '%$city%' AND status LIKE '%active%'") or die(mysql_error()); $total_rows = mysql_num_rows($data_p); } if ($city && $country) { $data_p = mysql_query("SELECT * FROM list WHERE city LIKE '%$city%' AND country LIKE '%$country%' AND status LIKE '%active%'") or die(mysql_error()); $total_rows = mysql_num_rows($data_p); } if ($city && $country && $postion_type) { $data_p = mysql_query("SELECT * FROM list WHERE city LIKE '%$city%' AND country LIKE '%$country%' AND postion_type LIKE '%$postion_type%' AND status LIKE '%active%'") or die(mysql_error()); $total_rows = mysql_num_rows($data_p); } ?> I guess my question is, is there a way to simplify this process...because this has become extremely long very quickly. All of my results are being displayed by GET variables and I am using pagination in order to display the results. Any help, guidance or direction is greatly appreciated. Thank you! Link to comment https://forums.phpfreaks.com/topic/132371-solved-there-has-to-be-an-easier-way-then-this/ Share on other sites More sharing options...
foxtrotwhiskey Posted November 12, 2008 Share Posted November 12, 2008 Hi networkthis, Absolutely. Its really easy! I'm just going to post a sample, if you have any questions just let me know! <?php $query = "SELECT * FROM list WHERE "; if ($city) $query .= "city LIKE '%$city%' AND "; if ($country) $query .= "country LIKE '%$country%' AND "; if ($postion_type) $query .= "postion_type LIKE '%$postion_type%' AND "; $query .= "status LIKE '%active%'"; $data_p = mysql_query($query) or die(mysql_error()); $total_rows = mysql_num_rows($data_p); ?> Its not the only way one can do it, but this way is pretty intuitive. Link to comment https://forums.phpfreaks.com/topic/132371-solved-there-has-to-be-an-easier-way-then-this/#findComment-688244 Share on other sites More sharing options...
networkthis Posted November 12, 2008 Author Share Posted November 12, 2008 thank you that helps a ton! Much more simplified...and now I see several other possibilities for how it can be done thanks!!!! Link to comment https://forums.phpfreaks.com/topic/132371-solved-there-has-to-be-an-easier-way-then-this/#findComment-688252 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.