katlis Posted July 3, 2008 Share Posted July 3, 2008 I have a form where, for the purposes of creating an entry, members can select multiple categories (with checkboxes) which is then stored as a comma separated value in a MySQL column called 'category'. I have a separate page with a function to browse ALL entries in ANY category, but I need to filter it when a user selects certain categories to search/browse (again, with checkboxes). I need to do something similar to: SELECT * FROM table WHERE category='cats' OR 'dogs' OR 'fish' How can I accomplish this if the column "category" could have values such as: row1: 'dogs,cats,birds' row2: 'fish,monkeys,birds,mice' etc.? Any help is greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/113131-select-from-csv-mysql-value/ Share on other sites More sharing options...
ratcateme Posted July 3, 2008 Share Posted July 3, 2008 you could have a query like: SELECT * FROM table WHERE category='%cats%' OR category='%dogs%' OR category='%fish%' % is for wildcard. Scott. Link to comment https://forums.phpfreaks.com/topic/113131-select-from-csv-mysql-value/#findComment-581179 Share on other sites More sharing options...
katlis Posted July 3, 2008 Author Share Posted July 3, 2008 Sounds good, that may work. However... I won't know how many times to use OR in the query since it's up to the user on how many categories they want to search (0 - 6). Link to comment https://forums.phpfreaks.com/topic/113131-select-from-csv-mysql-value/#findComment-581238 Share on other sites More sharing options...
ratcateme Posted July 4, 2008 Share Posted July 4, 2008 well if you have check boxes then you could have a script like this Form <input type="checkbox" name="categorys[]" value="cat" > <input type="checkbox" name="categorys[]" value="dog" > <input type="checkbox" name="categorys[]" value="fish" > <input type="checkbox" name="categorys[]" value="bird" > PHP <?php //make query $categorys = $_POST['categorys']; $where = array(); foreach($categorys as $value){ $where[] = "category='%{$value}%'"; } $where = implode(' OR ',$where); $query = "SELECT * FROM table WHERE {$where};"; ?> Scott. Link to comment https://forums.phpfreaks.com/topic/113131-select-from-csv-mysql-value/#findComment-581502 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.