|
-
Jun 24th, 2008, 04:08 AM
#1
Thread Starter
Lively Member
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!
-
Jun 24th, 2008, 11:03 PM
#2
Addicted Member
Re: csv export formatting
After fetching the data of the last column, replace the commas in that with a | . str_replace(',' '|', $value);
-
Jun 25th, 2008, 03:59 AM
#3
Thread Starter
Lively Member
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!
-
Jun 25th, 2008, 04:08 AM
#4
Addicted Member
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.'",';
}
-
Jun 26th, 2008, 04:52 PM
#5
Thread Starter
Lively Member
Re: csv export formatting
Thanks for your reply.
Not too sure where i would implement this within my code, any ideas?
thanks again
-
Jun 26th, 2008, 06:39 PM
#6
Thread Starter
Lively Member
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
-
Jun 26th, 2008, 10:01 PM
#7
Addicted Member
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);
-
Jun 27th, 2008, 03:23 AM
#8
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|