PHP Tutorial-php Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.