PHP converting mysql to excel -
working on php converting excel, having problems in excel file doesn't add each row in own cell. here how looks: https://www.dropbox.com/s/64lz7lqy8z7x72j/export.csv writes on first cell , looks has written of cell.
and here php code:
<?php $conn = mysql_connect('localhost', 'root', '') or die(mysql_error()); mysql_select_db('chart', $conn) or die(mysql_error($conn)); $result = mysql_query('select * chartgoogle', $conn) or die(mysql_error($conn)); header('content-type: text/csv'); // tell browser treat file csv header('content-disposition: attachment;filename=export.csv'); // tell browser download file in user's system name export.csv $row = mysql_fetch_assoc($result); // column names if ($row) { outputcsv(array_keys($row)); // wil pass column names outputcsv function } while ($row) { outputcsv($row); // loop used fetch rows table , pass them outputcsv func $row = mysql_fetch_assoc($result); } function outputcsv($fields) { $separator = ''; foreach ($fields $field) { echo $separator . $field; $separator = ','; // separate values comma } echo "\r\n"; //give carriage return , new line space after each record } ?>
try :
function outputcsv($fields) { $csv = ''; foreach ($fields $field) { $csv .= '"' . $field . '",'; } $csv .= "\r\n"; //give carriage return , new line space after each record echo $csv; }
take @ double quotes placed before , after field. csv files use double quotes closures. call text qualifier.
Comments
Post a Comment