我一直在研究数据库。现在是将数据导出到 csv 文件的最后一步。我已经创建了文件,它运行良好。但是现在的要求是可以更改保存在数据库中的值。我不知道如何解决这个问题。Bellow 是我使用自定义列名编写的代码。但只是说如果性别是 M,它应该在 csv 中导出男性而不是 M。我无法更改它在 MySQL 数据库中的保存方式,因为第二种导出类型要求它是 M 而不是男性。还有更多这样的列。
所以请谁能帮我解决这个问题并告诉我如何修改此代码以根据要求更改列的值。
$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);