Forum Moderators: coopster
What function should I use to get specific fields from my CSV? My table doesn't use unique ids, at least not like mysql, with a unique number identifying each row in the leftmost column. What then should I use to identify each row from which I'd like to get data?
I'm making a page that describes a person based on the query string and the information in the database, in which each person has a row. So example.com/example.php?christopher+walken would bring up a page where Walken's information is listed. So I guess the ideal way to do what I want is to query the CSV for the name in the query string (Christopher Walken), then get and print the other information in the row (tall, great guy, etc). Any suggestions on this query?
In your case; it looks like you want to read the file until a certain field contains a certain value. For example, if then name is in field 3 and you're looking for the "Christopher Walken" record, you would do something like this:
$found = false;$fp = fopen("filename.csv","r");
while($record = fgetcsv($fp))
{
// quit the loop when you find the record you want
if ($record[3] == "Christopher Walken") { $found = true; break; }
}fclose($fp);
if ($found)
{
// display $record here
}
else
{
print "Not found!";
}
Replace "Chrisopher Walken" with $_GET["name"] (if you're using?name= on your URL) and you should be in business...