fran1942 Posted October 14, 2008 Share Posted October 14, 2008 sorry for this newbie question. I have an html form with multiple selection options for a database search. These user-selected options are then being posted to the PHP script in the form of a CSV file ie. each search category is represented by one comma seperated value. How do I then parse this CSV file received by PHP, into a mySql "SELECT...WHERE..." query ? thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/128451-csv-post-submission/ Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 When you grab the data you need to use the explode() function so you can get each piece using the comma as a delimiter. For the query part I'm going to need more details... Quote Link to comment https://forums.phpfreaks.com/topic/128451-csv-post-submission/#findComment-665655 Share on other sites More sharing options...
fran1942 Posted October 15, 2008 Author Share Posted October 15, 2008 thanks, the exploded csv would result in (for instance) four different search subjects: cats dogs mice horses each of those subjects would be found in the column "animalType" within a table named "Animals". The search should return all relevant data in those categories. So, would I have to build a search query using multiple "WHERE" clauses made up of the above search phrases ? eg. SELECT * from animals WHERE Animals = "cats" and WHERE Animals = "dogs" etc. etc. ? Thanks for any help on most efficient way to do that. I guess some sort of loop ? Quote Link to comment https://forums.phpfreaks.com/topic/128451-csv-post-submission/#findComment-665690 Share on other sites More sharing options...
CroNiX Posted October 15, 2008 Share Posted October 15, 2008 $csv="dog,cat,baboon"; $where = ""; $animals = explode(",", $csv); $boundary=sizeof($animals); for($count = 0; $count < $boundary; $count++) { $where .= "animalType = '" . $animals[$count] . "'"; if($count < $boundary -1) $where .= " OR "; //I assume you wanted OR and not AND } $sql = "SELECT * from animals WHERE " . $where; echo $sql; I did this in a foreach loop the first time and for some reason it wasn't working properly and I didn't want to keep messing with it....but this outputs: SELECT * from animals WHERE animalType = 'dog' OR animalType = 'cat' OR animalType = 'baboon' Quote Link to comment https://forums.phpfreaks.com/topic/128451-csv-post-submission/#findComment-665800 Share on other sites More sharing options...
Maq Posted October 15, 2008 Share Posted October 15, 2008 How do I then parse this CSV file received by PHP, into a mySql "SELECT...WHERE..." query ? What exactly are you trying to accomplish? Quote Link to comment https://forums.phpfreaks.com/topic/128451-csv-post-submission/#findComment-666112 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.