blight Posted October 17, 2012 Share Posted October 17, 2012 Hi Everyone Struggling with something that should be incredibly simple I need to search through a csv file for a certain string and then return the rows which contain that string and only certain columns for that row So I have the simple part to display only the relevant columns but I cant find any info on how to search and only return the relevant rows? Your assistance would be highly appreciated. <?php if (($handle = fopen("filename.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { echo "$data[1] $data[2] $data[3] $data[18] $data[34] <br />\n"; } fclose($handle); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/269574-searching-through-a-csv-and-only-returning-certain-rows-and-certain-columns/ Share on other sites More sharing options...
Barand Posted October 17, 2012 Share Posted October 17, 2012 you could if (whateverIAmLookingFor) { echo "$data[1] etc"; } Quote Link to comment https://forums.phpfreaks.com/topic/269574-searching-through-a-csv-and-only-returning-certain-rows-and-certain-columns/#findComment-1385739 Share on other sites More sharing options...
blight Posted October 17, 2012 Author Share Posted October 17, 2012 Hi Barand Thanks - but I cant figure out the syntax to combine the if statement and the fgetcsv function... Quote Link to comment https://forums.phpfreaks.com/topic/269574-searching-through-a-csv-and-only-returning-certain-rows-and-certain-columns/#findComment-1385741 Share on other sites More sharing options...
warrickbayman Posted October 17, 2012 Share Posted October 17, 2012 (edited) Firstly, Your while loop will loop through the entire csv file. You can write an if statement to capture (or echo) the data you need. So, a small alteration could be... if (($handle = fopen("filename.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { if ($data[1] === <whateverIAmLookingFor>) { echo $data[2] . "\n"; // Or do whatever you need to do with that data. } } fclose($handle); } You could also grab the array in the while loop and work with it later (personally... I prefer this): if (($handle = fopen("filename.csv", "r")) !== FALSE) { $row=0; $csv_row = array(); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { if ($data[1] === <whateverIAmLookingFor>) { $csv_row = $data; } } fclose($handle); // Do whatever you need to with $csv_array... echo $csv_row[2] . "\n"; } If you're having problems reading in the CSV file (perhaps only returning one row), it could be funny line endings. Try setting: ini_set('auto_detect_line_endings', true); Edited October 17, 2012 by warrickbayman Quote Link to comment https://forums.phpfreaks.com/topic/269574-searching-through-a-csv-and-only-returning-certain-rows-and-certain-columns/#findComment-1385754 Share on other sites More sharing options...
Barand Posted October 17, 2012 Share Posted October 17, 2012 Hi Barand Thanks - but I cant figure out the syntax to combine the if statement and the fgetcsv function... As you don't tell us what you are searching for and in which column(s), neither can I. Quote Link to comment https://forums.phpfreaks.com/topic/269574-searching-through-a-csv-and-only-returning-certain-rows-and-certain-columns/#findComment-1385758 Share on other sites More sharing options...
jcanker Posted October 17, 2012 Share Posted October 17, 2012 Here's something I just used to do something similar. Mine looked for empty columns and replaced "" with NULL to prep for DB entry, but you should be able to get this to look for your values and respond appropriately: $file_handle = fopen("requirementsTable.csv", "r"); if($file_handle){echo " We opened requiremntsTable.csv! ";} while (!feof($file_handle) ) { $cellBlock = fgetcsv($file_handle, 1024); for($i=0; $i < count($cellBlock); $i++) { if($cellBlock[$i] == ""|| is_null($cellBlock[$i])){$cellBlock[$i]="NULL";} if($cellBlock[$i] != "NULL") { $cellBlock[$i] = addslashes($cellBlock[$i]); $cellBlock[$i] = "'".$cellBlock[$i]."'"; } // echo " I counter is: ".$i; } Quote Link to comment https://forums.phpfreaks.com/topic/269574-searching-through-a-csv-and-only-returning-certain-rows-and-certain-columns/#findComment-1385789 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.