Jump to content

csv POST submission


fran1942

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/128451-csv-post-submission/
Share on other sites

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 ?

Link to comment
https://forums.phpfreaks.com/topic/128451-csv-post-submission/#findComment-665690
Share on other sites

$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'
Link to comment
https://forums.phpfreaks.com/topic/128451-csv-post-submission/#findComment-665800
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.