Phpcsv operation code. Click the following link to download the csv operation class: 1. the code for generating csv file copy is as follows: require. includecsvdatafile. php; set_time_limit (200); header (click the address to download Conte: csv operation class
The usage instructions are as follows:
1. generate a csv file
The code is as follows:
Require "./include/csvdatafile. php ";
Set_time_limit (200 );
Header ("Content-type: application/RFC822 ");
Header ('content-Disposition: attachment; filename=export.csv ');
$ Arr_export_titles = array ("student ID", "student ID", "student name ");
$ Csvfile = new csvDataFile ("", "w ");
Echo $ csvfile-> printline ($ arr_export_titles );
// Method 1
$ Print_data1 [] = 1;
$ Print_datal [] = "039413301 ";
$ Print_data1 [] = "James ";
Echo $ csvfile-> printline ($ print_data1 );
$ Print_data2 [] = 2;
$ Print_data2 [] = "039413302 ";
$ Print_data2 [] = "Li Si ";
Echo $ csvfile-> printline ($ print_data2 );
$ Print_data3 [] = 3;
$ Print_data3 [] = "039413303 ";
$ Print_data3 [] = "Wang Wu ";
Echo $ csvfile-> printline ($ print_data3 );
// Method 2
$ Print_data [1] [] = 1;
$ Print_data [2] [] = "039413301 ";
$ Print_data [1] [] = "James ";
$ Print_data [2] [] = 2;
$ Print_data [2] [] = "039413302 ";
$ Print_data [2] [] = "Li Si ";
$ Print_data [3] [] = 3;
$ Print_data [3] [] = "039413303 ";
$ Print_data [3] [] = "Wang Wu ";
Echo $ csvfile-> printcsv ($ print_data );
2. open csv read data
Code
The code is as follows:
Require "./include/csvdatafile. php ";
$ Filename = "E:/development/csvfile/datefile.csv ";
// Read file source
$ Handle = fopen ($ filename, "r ");
$ Contents = fread ($ handle, filesize ($ filename ));
Fclose ($ handle );
// Format content for special chars
$ Contents = @ addslashes ($ contents );
$ Contents = @ str_replace ('\,', '\,', $ contents );
$ Contents = @ stripslashes ($ contents );
// Write to new file
$ Handle = @ fopen ($ filename, "w ");
@ Fwrite ($ handle, $ contents );
@ Fclose ($ handle );
$ Fd = @ fopen ($ filename, "rb ");
$ First_line = str_replace (',', str_replace ('"','', trim (@ fgets ($ fd, 1000 ))));
@ Fclose ($ fd );
If ($ first_line! = "Student ID, student ID, student name "){
$ Pass = false;
}
If ($ pass ){
$ Csv = new csvDataFile ($ filename );
While ($ csv-> next_Row ()){
$ Userid = trim ($ csv-> f ('student number '));
$ Classno = trim ($ csv-> f ('student id '));
$ Username = trim ($ csv-> f ('student name '));
}
}
Pipeline usage instructions: 1. Generate the csv file code as follows: require "./include/csvdatafile. php"; set_time_limit (200); header ("Conte...