file - CSV manipulation using php -
hey learning php , manipulating csv files here. i'm creating random key generator courses. adding new column keys course.
i have managed this. when run script row being read , key generated that. example if have 3 courses a,b,c 4 keys generated , added csv.
can have or tips on reading , writing csv?
<?php $file= fopen("input.csv","r"); $output=fopen("output.csv","w"); function gen_keys($length=10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; $characterslength = strlen($characters); $randomstring = ''; ($i = 0; $i < $length; $i++) { $randomstring .= $characters[rand(0, $characterslength - 1)]; } return $randomstring; } //echo gen_keys(); $list=array(); while(!feof($file)) { //print_r(fgetcsv($file)); $list=fgetcsv($file); //$list=array_filter($list); $list[]=gen_keys(); echo "<br />"; print_r($list); fputcsv($output, $list); //print_r(fgetcsv($output)); } fclose($file); fclose($output);?>
input : comp1400
comp2000
info2425
eng1234
here updated , working version:
$list=fgetcsv($file); if (!empty($list)) { //$list=array_filter($list); $list[]=gen_keys(); echo "<br />"; print_r($list); fputcsv($output, $list); }
please note line if (!empty($list)) {
checks if have imput.
more information empty()
in documentation.
Comments
Post a Comment