【问题标题】:how to change values of csv while exporting in php如何在 php 中导出时更改 csv 的值
【发布时间】:2013-11-06 16:05:29
【问题描述】:

我一直在研究数据库。现在是将数据导出到 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);

【问题讨论】:

    标签: php mysql csv mysqli export-to-csv


    【解决方案1】:

    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
    
    }
    

    【讨论】:

      【解决方案2】:

      在您的查询中,类似于:

      SELECT IF(student_information.gender = 'M' then 'Male' else 'Female') .....
      

      或者,您可以创建一个包含 M 和男性行以及 F 和女性行的连接表,但这可能有点矫枉过正。

      我很老套,只假设有两种性别。

      编辑:SELECT IF(gender='M','Male','Female') ... FROM student_information ...

      【讨论】:

      • 也缩短了:IF(student_information.gender='M','Male','Female')
      • 您还可以通过使用 FROM 子句指定表来缩短您的选择语句:SELECT FROM student_information oen, first_name, second_name, ...
      • 哎呀,语法错误(大声笑):SELECT oen, first_name, ... FROM student_information
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 2022-07-20
      • 1970-01-01
      • 2022-07-06
      相关资源
      最近更新 更多