refiking Posted November 16, 2008 Share Posted November 16, 2008 I need to insert an id on each row of a csv file that is auto-increment and I need it to be the first column. Is there a way in PHP that I can do this? Link to comment https://forums.phpfreaks.com/topic/132903-solved-how-to-write-id-on-csv-file/ Share on other sites More sharing options...
ratcateme Posted November 16, 2008 Share Posted November 16, 2008 $file = file("file.csv"); for($i = 0;$i < count($file); $i++){ $file[$i] = ($i+1) . "," . $file[$i]; } file_put_contents("file.csv",implode("\r\n",$file)); should do it Scott. Link to comment https://forums.phpfreaks.com/topic/132903-solved-how-to-write-id-on-csv-file/#findComment-691083 Share on other sites More sharing options...
refiking Posted November 16, 2008 Author Share Posted November 16, 2008 Here's what that returned: Call to undefined function: file_put_contents() Here's the script used: <?php $file = file("file.csv"); for($i = 0;$i < count($file); $i++){ $file[$i] = ($i+1) . "," . $file[$i]; } if(file_put_contents("file.csv",implode("\r\n",$file))){ Echo 'Successful'; } ELSE{ Echo 'Uh Oh!'; } ?> Link to comment https://forums.phpfreaks.com/topic/132903-solved-how-to-write-id-on-csv-file/#findComment-691085 Share on other sites More sharing options...
ratcateme Posted November 16, 2008 Share Posted November 16, 2008 try <?php $file = file("file.csv"); for($i = 0;$i < count($file); $i++){ $file[$i] = ($i+1) . "," . $file[$i]; } // if($fh = fopen("file.csv","w")){ fwrite($fh,implode("\r\n",$file)); echo 'Successful'; fclose($fh); }else{ echo 'Uh Oh!'; } ?> Scott. Link to comment https://forums.phpfreaks.com/topic/132903-solved-how-to-write-id-on-csv-file/#findComment-691087 Share on other sites More sharing options...
refiking Posted November 16, 2008 Author Share Posted November 16, 2008 What does this mean? Allowed memory size of 12582912 bytes exhausted (tried to allocate 7262 bytes) Link to comment https://forums.phpfreaks.com/topic/132903-solved-how-to-write-id-on-csv-file/#findComment-691088 Share on other sites More sharing options...
ratcateme Posted November 16, 2008 Share Posted November 16, 2008 how big is the csv file? what is the line number? Scott. Link to comment https://forums.phpfreaks.com/topic/132903-solved-how-to-write-id-on-csv-file/#findComment-691089 Share on other sites More sharing options...
refiking Posted November 16, 2008 Author Share Posted November 16, 2008 5MB There are hundreds of lines and each one needs an id Link to comment https://forums.phpfreaks.com/topic/132903-solved-how-to-write-id-on-csv-file/#findComment-691092 Share on other sites More sharing options...
ratcateme Posted November 16, 2008 Share Posted November 16, 2008 try adding ini_set("memory_limit","64M"); to the top it looks like the script is running out of memory as it is only allowed 12MB Link to comment https://forums.phpfreaks.com/topic/132903-solved-how-to-write-id-on-csv-file/#findComment-691093 Share on other sites More sharing options...
refiking Posted November 16, 2008 Author Share Posted November 16, 2008 Ok. So, it added the id perfectly. Only problem is it created an additional line between rows. Any way to do it without those rows there? Link to comment https://forums.phpfreaks.com/topic/132903-solved-how-to-write-id-on-csv-file/#findComment-691100 Share on other sites More sharing options...
ratcateme Posted November 16, 2008 Share Posted November 16, 2008 try changing fwrite($fh,implode("\r\n",$file)); to fwrite($fh,implode($file)); Scott. Link to comment https://forums.phpfreaks.com/topic/132903-solved-how-to-write-id-on-csv-file/#findComment-691108 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.