bores_escalovsk Posted May 16, 2014 Share Posted May 16, 2014 (edited) i have to read a single line from a csv, its a really big file and i only need one column. i need the response to be a string ,i made a search and found the following code but i dont have any idea how to get a single line from a single string per run . <?php $row = 1; //open the file if (($handle = fopen("file.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 0, ",")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "<br />\n"; } } fclose($handle); } ?> Edited May 16, 2014 by bores_escalovsk Quote Link to comment Share on other sites More sharing options...
bores_escalovsk Posted May 16, 2014 Author Share Posted May 16, 2014 if i take out the counter it will read one line only ? but on next run it will read same line? Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted May 17, 2014 Share Posted May 17, 2014 (edited) if you need to find something specific, wrap it with an if statement, perform any search or checks on it and then echo or save as a variable. examples: if($data[$c] != ''){ $string = $data[$c]; echo $string . "<br />\n"; } or if to make them a new array $new_array = array();//outside the loop if($data[$c] != ''){ $new_array[] = $data[$c]; } Edited May 17, 2014 by QuickOldCar Quote Link to comment Share on other sites More sharing options...
.josh Posted May 17, 2014 Share Posted May 17, 2014 what are you trying to do... are you trying to just echo out the value of only 1 column? Are you trying to put it into a db? Quote Link to comment Share on other sites More sharing options...
bores_escalovsk Posted May 17, 2014 Author Share Posted May 17, 2014 what are you trying to do... are you trying to just echo out the value of only 1 column? Are you trying to put it into a db? its a part of my code , the echo is only for testing ...the code needs to read 1 line per run ,return the value as a string and at the next run read the next line. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted May 17, 2014 Share Posted May 17, 2014 If it's a large file you should save the csv to a database and query each result that way Having to load a very large file each time takes lots of resources. Quote Link to comment Share on other sites More sharing options...
Solution .josh Posted May 17, 2014 Solution Share Posted May 17, 2014 Okay, sounds like a generator is what you want. If you aren't using php 5.5+ then you can achieve similar with an iterator. Quote Link to comment Share on other sites More sharing options...
bores_escalovsk Posted May 17, 2014 Author Share Posted May 17, 2014 this is worst than the time i learned regex ,-, If it's a large file you should save the csv to a database and query each result that way Having to load a very large file each time takes lots of resources. i was trying to do that, but the file have 2gb , not the easyest thing Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 17, 2014 Share Posted May 17, 2014 define: per run/next run? do you mean, read the next line in the file, one at a time, each time the page gets requested? Quote Link to comment Share on other sites More sharing options...
.josh Posted May 17, 2014 Share Posted May 17, 2014 this is worst than the time i learned regex ,-, I don't mean to be offensive but regex is one of the hardest things to master (in any language). If you managed that then I'm not sure how a couple of simple loops is troubling you... Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted May 17, 2014 Share Posted May 17, 2014 this is worst than the time i learned regex ,-, i was trying to do that, but the file have 2gb , not the easyest thing Split the csv file up then try it http://sourceforge.net/projects/splitcsv/ Quote Link to comment Share on other sites More sharing options...
bores_escalovsk Posted May 17, 2014 Author Share Posted May 17, 2014 Split the csv file up then try it http://sourceforge.net/projects/splitcsv/ just for the record , i solved by using dbforge and transforming the csv in a mysql table, way easier than what i was trying to do. thank you all guys Quote Link to comment 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.