Cambio de valores al generar CSV

I am generating a CSV file containing data from a MYSQL database. There are some fields that contain boolean values - 0 and 1.
Want I want is to change the boolean to some string and in the resulting file.

Update :: CSV generation code

function generateCSV($query, $filename = 'export.csv') {
    $csv_terminated = "\n";
    $csv_separator = ",";
    $csv_enclosed = '"';
    $csv_escaped = "\\";
    $sql_query = $query;

    // Gets the data from the database
    $result = mysql_query($sql_query);
    $fields_cnt = mysql_num_fields($result);

    $schema_insert = '';

    for ($i = 0; $i < $fields_cnt; $i++) {
        $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
        $schema_insert .= $l;
        $schema_insert .= $csv_separator;
    } // end for

    $out = trim(substr($schema_insert, 0, -1));
    $out .= $csv_terminated;

    // Format the data
    while ($row = mysql_fetch_array($result)) {
        $schema_insert = '';
        for ($j = 0; $j < $fields_cnt; $j++) {
            if ($row[$j] == '0' || $row[$j] != '') {

                if ($csv_enclosed == '') {
                    $schema_insert .= $row[$j];
                } else {
                    $schema_insert .= $csv_enclosed .
                            str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
                }
            } else {
                $schema_insert .= '';
            }

            if ($j < $fields_cnt - 1) {
                $schema_insert .= $csv_separator;
            }
        } // end for

        $out .= $schema_insert;
        $out .= $csv_terminated;
    } // end while

    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Length: " . strlen($out));
    header("Content-type: text/x-csv");
    header("Content-Disposition: attachment; filename=$filename");
    echo $out;
}

preguntado el 09 de marzo de 12 a las 14:03

Can you post the code you use to generate the csv? -

I have updated the question. Kindly check -

4 Respuestas

For these columns use the CASE syntax.

SELECT
    CASE your_column
       WHEN 0 THEN 'zero or any other string'
       WHEN 1 THEN 'one or any other string'
    END AS your_alias
FROM
    your_table_name

respondido 09 mar '12, 14:03

Here you go. I cannot guess your full query but my example now gives some more context. - Leif

I guess you want to do replace some kind of status with text? So why not simply check it?

if ($foo == 0) { echo 'No'; } else { echo 'Yes'; }

respondido 09 mar '12, 14:03

You can use CASE syntax:

select case when column = 0 then 'somestring_0' when value0 = 1 then 'somestring_1' end from table

http://dev.mysql.com/doc/refman/5.0/en/case-statement.html

respondido 09 mar '12, 14:03

Or you could use the MySQL IF and REPLACE functions to replace the values according to your preferences, as an alternative to the CASE solutions.

respondido 09 mar '12, 14:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.