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 Quote Link to comment 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"); ?> Quote Link to comment 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 Quote Link to comment 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"); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.