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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.