lordrt Posted August 4, 2009 Share Posted August 4, 2009 I have a csv file which contains values as follows (the line numbers are not found in the file): line1: 1, 2, 3, 4, 5, 6, 7, 8 line2: 9, 10, 11, 12, ..... .... lineN: ................... I have to read the file one line at a time, and for each line read there will be a process which will be executed. Am trying the fgetcsv function but not really understanding the concept behind it. Can anyone help with some codes on how to call, open and read the csv file?? Link to comment https://forums.phpfreaks.com/topic/168772-read-a-csv-file-line-by-line/ Share on other sites More sharing options...
trq Posted August 4, 2009 Share Posted August 4, 2009 I'm not sure we could write better example than the one in the manual.... <?php $row = 1; $handle = fopen("test.csv", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== 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); ?> What part don't you get? Link to comment https://forums.phpfreaks.com/topic/168772-read-a-csv-file-line-by-line/#findComment-890423 Share on other sites More sharing options...
lordrt Posted August 4, 2009 Author Share Posted August 4, 2009 Does the above code read line-by-line, coz i have to read a line, do some ops on it, then read a new line and so on Link to comment https://forums.phpfreaks.com/topic/168772-read-a-csv-file-line-by-line/#findComment-890427 Share on other sites More sharing options...
phpSensei Posted August 4, 2009 Share Posted August 4, 2009 Does the above code read line-by-line, coz i have to read a line, do some ops on it, then read a new line and so on <?php $handle = fopen("file.txt","r"); if($handle){ while(!feof($handle)){ $line = fgets($handle,1024); // line1: 1, 2, 3, 4, 5, 6, 7, 8 } fclose($handle); } ?> Link to comment https://forums.phpfreaks.com/topic/168772-read-a-csv-file-line-by-line/#findComment-890434 Share on other sites More sharing options...
Mark Baker Posted August 4, 2009 Share Posted August 4, 2009 Does the above code read line-by-line, coz i have to read a line, do some ops on it, then read a new line and so onYes! Each call to fgetcsv() reads a single line from the file Link to comment https://forums.phpfreaks.com/topic/168772-read-a-csv-file-line-by-line/#findComment-890439 Share on other sites More sharing options...
lordrt Posted August 4, 2009 Author Share Posted August 4, 2009 in the example i gave above, for each line it ends with something like a "\n" if converted to html, so do I have to include this in the php script as well so as to know where a line ends Link to comment https://forums.phpfreaks.com/topic/168772-read-a-csv-file-line-by-line/#findComment-890446 Share on other sites More sharing options...
trq Posted August 4, 2009 Share Posted August 4, 2009 in the example i gave above, for each line it ends with something like a "\n" if converted to html, so do I have to include this in the php script as well so as to know where a line ends What example you gave above? You haven't posted any code. Link to comment https://forums.phpfreaks.com/topic/168772-read-a-csv-file-line-by-line/#findComment-890456 Share on other sites More sharing options...
lordrt Posted August 4, 2009 Author Share Posted August 4, 2009 sorry, code goes like this: <?php //import php file: import_article.php require("import_article.php"); // mysql_connect("127.0.0.1", "root", "") or die (mysql_error()); mysql_select_db("drupal_db") or die (mysql_error()); //read first .csv file encountered $path = "sites/path/files/"; $sfile = "*.csv"; foreach (glob($path.$sfile) as $filename){ echo $filename . "<br/>"; $file = fopen($filename, 'r'); while (($data = fgetcsv($file, 1000, ",")) !== False) { $result = ImportArticle($data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6]); unset ($result); } echo "read complete"; fclose($file); //unlink ($filename); } //echo "closing connection"; mysql_close(); ?> The 'ImportArticle' is a function found in the php file being called by require() which contains php mysql statements to update/insert data into tables An example of my csv is (exclude line1,2) and text are not wrapped in editor: line1: 103, 'Ball. Rustique', '<p><img width="500" height="300" src="/.../103.jpg" alt="" /></p><p> </p>Ball. Rustique', 0.8, '103.jpg', '/.../103.jpg', 0 line2: 60, 'Petit pain allongé m', '<p>Petit pain allongé m</p>', 0.6, ' ', ' ', 0 Link to comment https://forums.phpfreaks.com/topic/168772-read-a-csv-file-line-by-line/#findComment-890461 Share on other sites More sharing options...
Garethp Posted August 4, 2009 Share Posted August 4, 2009 They already gave you the perfect thing <?php $handle = fopen("file.txt","r"); if($handle){ while(!feof($handle)){ $line = fgets($handle,1024); // line1: 1, 2, 3, 4, 5, 6, 7, 8 } fclose($handle); } ?> Now I suggest you actually try it for yourself instead of asking other people to do your testing for you. You should really comeback if you have problems, not because you don't feel like testing it Link to comment https://forums.phpfreaks.com/topic/168772-read-a-csv-file-line-by-line/#findComment-890462 Share on other sites More sharing options...
Mark Baker Posted August 4, 2009 Share Posted August 4, 2009 in the example i gave above, for each line it ends with something like a "\n" if converted to html, so do I have to include this in the php script as well so as to know where a line endsNo! fgetcsv() reads only a single line from the file each time it is called. Link to comment https://forums.phpfreaks.com/topic/168772-read-a-csv-file-line-by-line/#findComment-890463 Share on other sites More sharing options...
phpSensei Posted August 4, 2009 Share Posted August 4, 2009 Use the script I provided for you <?php $handle = fopen("file.txt","r"); if($handle){ while(!feof($handle)){ $line = fgets($handle,1024); // line1: 1, 2, 3, 4, 5, 6, 7, 8 } fclose($handle); } ?> Link to comment https://forums.phpfreaks.com/topic/168772-read-a-csv-file-line-by-line/#findComment-890464 Share on other sites More sharing options...
lordrt Posted August 4, 2009 Author Share Posted August 4, 2009 thanks for ur help guys, for reading a text file with fgets and doing the ops on a db works fine, it was only the csv which confused me somehow. In fact I was using the same code below to read my data from .txt files, but now was asked to use csv to do the reading of data [pre]<?php //connecting to host db mysql_connect("127.0.0.1", "root", "") or die (mysql_error()); echo "Connected to host" . "<br/>"; mysql_select_db("drupal_db") or die(mysql_error()); echo "Connected to DB" . "<br/>"; //read first .ark file encountered $path = "sites/zenhaeusern-sion.sw/files/Arkeio/"; $sfile = "*.txt"; foreach (glob($path.$sfile) as $filename){ echo $filename . "<br/>"; $file = fopen($filename, 'r'); while (!feof($file)){ $data = fgets($file); echo $data . "<br/>"; mysql_query($data); unset ($data); } echo "read complete"; fclose($file); unlink ($filename); break; } echo "closing connection"; mysql_close(); ?> [/pre] Link to comment https://forums.phpfreaks.com/topic/168772-read-a-csv-file-line-by-line/#findComment-890485 Share on other sites More sharing options...
lordrt Posted August 4, 2009 Author Share Posted August 4, 2009 sorry, code goes like this: <?php //import php file: import_article.php require("import_article.php"); // mysql_connect("127.0.0.1", "root", "") or die (mysql_error()); mysql_select_db("drupal_db") or die (mysql_error()); //read first .csv file encountered $path = "sites/path/files/"; $sfile = "*.csv"; foreach (glob($path.$sfile) as $filename){ echo $filename . "<br/>"; $file = fopen($filename, 'r'); while (($data = fgetcsv($file, 1000, ",")) !== False) { $result = ImportArticle($data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6]); unset ($result); } echo "read complete"; fclose($file); //unlink ($filename); } //echo "closing connection"; mysql_close(); ?> The 'ImportArticle' is a function found in the php file being called by require() which contains php mysql statements to update/insert data into tables An example of my csv is (exclude line1,2) and text are not wrapped in editor: line1: 103, 'Ball. Rustique', '<p><img width="500" height="300" src="/.../103.jpg" alt="" /></p><p> </p>Ball. Rustique', 0.8, '103.jpg', '/.../103.jpg', 0 line2: 60, 'Petit pain allongé m', '<p>Petit pain allongé m</p>', 0.6, ' ', ' ', 0 The code above is running only for the 1st line, it does not erad 2nd line, any help plz? Link to comment https://forums.phpfreaks.com/topic/168772-read-a-csv-file-line-by-line/#findComment-890509 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.