Results 1 to 8 of 8

Thread: csv export formatting

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    98

    csv export formatting

    Hi all,

    I am using the following code to export a table of data from SQL to CSV format.

    Code:
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=UserData.csv");
    header("Pragma: no-cache");
    header("Expires: 0");
    
    
    mysql_select_db($database_conf, $conf);
    $select = "SELECT username, email, , organisation, selections FROM users";
    $export = mysql_query($select);
    $count = mysql_num_fields($export);
    for ($i = 0; $i < $count; $i++) {
    $header .= mysql_field_name($export, $i).",";
    }
    while($row = mysql_fetch_row($export)) {
    $line = '';
    foreach($row as $value) {
    if ((!isset($value)) OR ($value == "")) {
    $value = "\t";
    } else {
    $value = str_replace('"', '""', $value);
    $value = '"' . $value . '"' . ",";
    }
    $line .= $value;
    }
    $data .= trim($line)."\n";
    }
    $data = str_replace("\r", "", $data);
    if ($data == "") {
    $data = "\n(0) Records Found!\n";
    }
    print "$header\n$data";
    exit;
    This works great however the final column 'selections' may sometimes be in the format of 21, 34, 56 or in a single number.

    Is it possible to output the values stored with commas into separate columns. e.g...

    |Selections| | |
    --------------------------
    | 21 | 11 | 40 |

    Thanks!

  2. #2
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Re: csv export formatting

    After fetching the data of the last column, replace the commas in that with a | . str_replace(',' '|', $value);

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    98

    Re: csv export formatting

    Hi,

    thanks for your reply, not sure i explained myself correctly though.

    I don't want to replace the commas with a |. I need to split the commad values up into their own columns.

    So for instance instead of this in Excel:



    i want it to look like:



    thanks again!

  4. #4
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Re: csv export formatting

    It is a csv file right? Commas in the column should automatically put them in separate cells. But since you are using " to enclose the values that might not be happening. What you can do is split the string at commas and assign them like you are doing the regular values.
    Code:
    $tval = explode(',', $value);
    foreach($tval as $v){
      $value .= '"'.$v.'",';
    }

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    98

    Re: csv export formatting

    Thanks for your reply.

    Not too sure where i would implement this within my code, any ideas?

    thanks again

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    98

    Question Re: csv export formatting

    I added the following to the code:

    Code:
    while($row = mysql_fetch_row($export)) {
    $line = '';
    $a = 0;
    foreach($row as $value) {
    if ((!isset($value)) OR ($value == "")) {
    $value = "\t";
    } else {
    $value = str_replace('"', '""', $value);
    //$value = '"' . $value . '"' . ",";
    $tval = explode(',', $value);
    $value .= $tval[$a].',';
    $a = $a + 1;
    }
    $line .= $value;
    }
    $data .= trim($line)."\n";
    }
    $data = str_replace("\r", "", $data);
    This separates the values into separate columns but for some reason the first column is repeating itself, for example the user Joe Bloggs would be displayed as: Joe BloggsJoe Bloggs

    help with this si appreciated,

    thanks

  7. #7
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Re: csv export formatting

    PHP Code:
    while($row mysql_fetch_row($export)) {
        
    $line '';
        
    $count count($row);
        for(
    $i=0;$i<$count;$i++){
            if ((!isset(
    $row[$i])) OR ($row[$i] == "")) {
                
    $value "\t";
            } else {
                if (
    $i == ($count-1)){ //last column
                    
    $tval explode(','$row[$i]);
                    
    $value '';
                    foreach(
    $val as $v)
                        
    $value .= '"'.$v.'",';
                }
                else{    
                    
    $value str_replace('"''""'$row[$i]);
                    
    $value '"' $value '"' ",";
                }        
            }
            
    $line .= $value;
        }
        
    $data .= trim($line)."\n";
    }
    $data str_replace("\r"""$data); 

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    98

    Re: csv export formatting

    Thanks very much srisa, that works great!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width