如何在php中导出时更改csv的值
我一直在研究一个数据库。现在是将数据导出到csv文件的最后一步。我已经创建了这个文件,它工作得非常好。但现在的要求是可以更改保存在数据库中的值。我不知道该怎么做。下面是我用自定义列名编写的代码。但是如果性别是M,它应该在csv中导出Male而不是M。我不能更改它在MySQL数据库中的保存方式,因为第二种类型的导出要求它不是Male。还有很多类似的专栏 所以,请有人能帮我解决这个问题,告诉我如何修改这个代码来根据需求更改列的值如何在php中导出时更改csv的值,php,mysql,csv,mysqli,export-to-csv,Php,Mysql,Csv,Mysqli,Export To Csv,我一直在研究一个数据库。现在是将数据导出到csv文件的最后一步。我已经创建了这个文件,它工作得非常好。但现在的要求是可以更改保存在数据库中的值。我不知道该怎么做。下面是我用自定义列名编写的代码。但是如果性别是M,它应该在csv中导出Male而不是M。我不能更改它在MySQL数据库中的保存方式,因为第二种类型的导出要求它不是Male。还有很多类似的专栏 所以,请有人能帮我解决这个问题,告诉我如何修改这个代码来根据需求更改列的值 $host = 'localhost'; // MYSQL datab
$host = 'localhost'; // MYSQL database host adress
$db = 'db_eschool'; // MYSQL database name
$user = 'admin'; // Mysql Datbase user
$pass = 'secretdatabase'; // Mysql Datbase password
$link = mysql_connect($host, $user, $pass); // Connect to the database
mysql_select_db($db);
function cleanData(&$str) {
if (strstr($str, '"'))
$str = '"' . str_replace('"', '""', $str) . '"'; // escape fields that include double quotes
}
$colnames = array(
'oen' => "OEN",
'first_name' => "Legal First name",
'second_name' => "Legal Second Name",
'last_name' => "Legal Last name",
'native_language' => "Language First Spoken",
'birth_date' => "Birth Date",
'gender' => "Gender",
'school_number' => "School Number",
'osr' => "Main School",
'postal_code' => "Postal Code",
'canadian_citizen' => "Status in Canada",
'date_entry' => "Year of Entry",
'start_date' => "Enrolment Start Date",
'end_date' => "Enrolment End Date",
'literacy_status' => "Literacy Status",
'com_inv_hours' => "Community Involvement Hours to Date",
'oces_course' => "Ministry Course Code",
'start_date' => "Course Start Date",
'end_date' => "Course End Date",
'credit_earned' => "Earned Credit Value",
'ft_marks' => "Final Mark",
'course_status' => "Course Complete",
'repeated_course' => "Repeated Course",
'oces_dip' => "Diploma Issued",
'issue_date' => "Date Issue");
function map_colnames($input) {
global $colnames;
return isset($colnames[$input]) ? $colnames[$input] : $input;
}
$flag = false;
$result = mysql_query("SELECT student_information.oen, student_information.first_name, student_information.second_name, student_information.last_name, student_information.native_language, student_information.birth_date, student_information.gender, student_information.school_number, student_information.osr, student_information.postal_code, student_information.canadian_citizen, student_information.date_entry, course_information.start_date, course_information.end_date, student_information.literacy_status, student_information.com_inv_hours, course_information.oces_course, course_information.credit_earned, course_information.ft_marks, course_information.course_status, course_information.repeated_course, course_information.cr_language, student_information.oces_dip, student_information.issue_date FROM student_information, course_information WHERE student_information.searcher = 'y' AND student_information.student_number = course_information.student_number ") or die('Query failed!');
$filename = 'OnSIS_export.csv';
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// Output to browser with appropriate mime type, you choose ;)
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$filename");
$out = fopen("php://output", 'w');
// filename for download
while (false !== ($row = mysql_fetch_assoc($result))) {
if (!$flag) { // display field/column names as first row
$firstline = array_map("map_colnames", array_keys($row));
fputcsv($out, $firstline, ',', '"');
$flag = true;
}
array_walk($row, 'cleanData');
fputcsv($out, array_values($row), ',', '"');
}
fclose($out);
在您的查询中,类似于:
SELECT IF(student_information.gender = 'M' then 'Male' else 'Female') .....
或者,您可以创建一个包含M和阳行以及F和阴行的联接表,但这可能有些过分
我很守旧,只假设有两种性别
编辑:
选择是否(性别=M'、'Male'、'Female')。。。从student\u information…
array\u walk
实际上可以为您提供数组中项目的键及其值。因此,您可以在cleanData
函数中执行类似操作,在该函数中,您可以检查数组索引,如果它是一个值需要更改的函数,则更新$str
:
function cleanData(&$str, $key) {
switch($key) {
case 'gender':
if ($str=='M') $str= 'Male';
elseif($str=='F') $str= 'Female';
break;
// ... other cases
}
if (strstr($str, '"'))
$str = '"' . str_replace('"', '""', $str) . '"'; // escape fields that include double quotes
}
也可以缩短:
IF(student\u information.gender='M','Male','Female')
您还可以通过使用FROM子句来指定表格来缩短select语句:select FROM student\u information oen,first\u name,second\u name,…oops,错误语法(lol):select oen,first\u name。。。来自学生信息