adamf Posted July 29, 2007 Share Posted July 29, 2007 Hi, I have a website with a database full of category names...I want to be able to chose lets say 5 of these categories and use a select from database clause where I could simply enter the category names I want to use but am not sure how I would go about doing it. I can get the list to display just the Aprilia category using the code below: $data1->q("SELECT * FROM categories WHERE cat_name = 'Aprilia' ORDER BY cat_name asc"); However...how could I get my site to display Aprilia, Ducati, Honda, Suzuki, Yamaha? Adam Link to comment https://forums.phpfreaks.com/topic/62346-multiple-select-from-database-where-clause/ Share on other sites More sharing options...
GingerRobot Posted July 29, 2007 Share Posted July 29, 2007 Use the implode() function on an array to create a list of possible values to use with the IN syntax in your query: <?php $cats = array('Aprilia', 'Ducati', 'Honda', 'Suzuki', 'Yamaha');//an array of the possible categories. I assume this might depend on form input. $data1->q("SELECT * FROM categories WHERE cat_name = IN(."implode(',',$cats)".) ORDER BY cat_name asc"); ?> Link to comment https://forums.phpfreaks.com/topic/62346-multiple-select-from-database-where-clause/#findComment-310251 Share on other sites More sharing options...
adamf Posted July 29, 2007 Author Share Posted July 29, 2007 I get the following error when adding in the code? Parse error: syntax error, unexpected T_STRING in /home/used/public_html/cont/FrontControl.php on line 848 Adam Link to comment https://forums.phpfreaks.com/topic/62346-multiple-select-from-database-where-clause/#findComment-310258 Share on other sites More sharing options...
Barand Posted July 29, 2007 Share Posted July 29, 2007 IF you have an array of strings the implode needs more than just a comma <?php $cats = array('Aprilia', 'Ducati', 'Honda', 'Suzuki', 'Yamaha'); $catlist = implode ("','", $cats); $data1->q("SELECT * FROM categories WHERE cat_name = IN ('$catlist') ORDER BY cat_name asc"); Link to comment https://forums.phpfreaks.com/topic/62346-multiple-select-from-database-where-clause/#findComment-310290 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.