Home > Backend Development > PHP Tutorial > php csv operation code_PHP tutorial

php csv operation code_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 15:42:52
Original
873 people have browsed it

Please click the following address to download: csv operation class
instructions are as follows:

1. Generate csv file

Copy code 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 number","student number","student name");

$csvfile = new csvDataFile("", ",", "w");
echo $csvfile->printline($arr_export_titles);
//Method 1
$print_data1[] = 1;
$print_data1[] = "039413301";
$print_data1[] = "Zhang San";
echo $csvfile->printline($print_data1);

$ print_data2[] = 2;
$print_data2[] = "039413302";
$print_data2[] = "李思";
echo $csvfile->printline($print_data2);


$print_data3[] = 3;
$print_data3[] = "039413303";
$print_data3[] = "王五";
echo $csvfile->printline($print_data3 );


//Method 2
$print_data[1][] = 1;
$print_data[1][] = "039413301";
$print_data[1 ][] = "Zhang San";
$print_data[2][] = 2;
$print_data[2][] = "039413302";
$print_data[2][] = "Li Four";
$print_data[3][] = 3;
$print_data[3][] = "039413303";
$print_data[3][] = "王五";
echo $csvfile->printcsv($print_data);





2. Open csv to read data



Code
Copy 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 number, student number, 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 Number'));
$username = trim($csv->f('Student Name'));
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320923.htmlTechArticlePlease click the following address to download: The instructions for using the csv operation class are as follows: 1. Generate the csv file and copy the code. The code is as follows: require "./include/csvdatafile.php"; set_time_limit(200); header("Conte...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template