0

私はデータベースに取り組んできました。データをcsvファイルにエクスポートする最後のステップです。ファイルを作成しましたが、完全に機能します。しかし、現在の要件は、データベースに保存されている値を変更できることです。これにアプローチする方法がわかりません。以下は、カスタム列名で書いたコードです。しかし、性別がMの場合、MではなくMaleをcsvでエクスポートする必要があります. このようなコラムは他にもたくさんあります。

したがって、これを解決するのを手伝ってくれる人がいて、このコードを変更して要件に従って列の値を変更する方法を教えてください。

$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);
4

2 に答える 2

0

array_walk実際には、配列内のアイテムのキーとその値を提供できます。したがって、cleanData関数で次のようなことを行うことができます。ここでは、配列インデックスをチェックし、値を変更する必要がある場合は update $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

}
于 2013-11-06T16:17:58.440 に答える
0

あなたのクエリでは、次のようなものです:

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

または、M と男性の行と F と女性の行を含む結合テーブルを作成することもできますが、それはやり過ぎかもしれません。

私は時代遅れで、性別は 2 つしかないと思っています。

編集:SELECT IF(gender='M','Male','Female') ... FROM student_information ...

于 2013-11-06T16:14:55.423 に答える