我一直在研究数据库。现在是将数据导出到csv文件的最后一步。我创建了文件,它完美地运行。但现在要求是可以更改数据库中保存的值。我不知道如何处理这个问题。 Bellow是我用自定义列名编写的代码。但只是说如果性别是M,它应该在csv中输出Male而不是M.我无法更改它在MySQL数据库中的保存方式,因为第二种类型的导出要求它是M而不是Male。并且还有更多这样的列。
所以请有人帮我解决这个问题并告诉我如何修改此代码以根据要求更改列的值。
$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);
答案 0 :(得分:0)
在您的查询中,例如:
SELECT IF(student_information.gender = 'M' then 'Male' else 'Female') .....
或者你可以创建一个包含M和Male行以及F和Female行的连接表,但这可能有点过分。
我很老式,只假设两个性别。
编辑:SELECT IF(gender='M','Male','Female') ... FROM student_information ...
答案 1 :(得分:0)
array_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
}