Click to See Complete Forum and Search --> : tab delimiter in csv file


raj_2006
10-25-2006, 12:24 AM
Hi all

I have posted similar type thread previously but now i want to know some thing more about the tab delimiter.

I have few records in the mysql db and i am exporting those records through a csv file in excel format.

My question:

1.Cant i use tab as a delimiter while exporting the records in a csv file?


say,

I have these records

Name age products

A 34 B,C,D
E 35 F,G,H

Now these commas(B,C,D) will be stay in the excel file but will be separated by a tab...........How is it possible?

My php code is here:
$file_pointer = fopen("sample.csv", "w");
fwrite($file_pointer,"");
fwrite($file_pointer,$string);
fclose($file_pointer);
$file_pointer=fopen("sample.csv", "w");
fwrite($file_pointer,"ICODE,STK,POC,BOCC,BOCU,TYPE,DESIGN,D1,D2,HT1,HT2,LIST,CID\n");//shall i use tab here if so then how?
$sql_email ="select * from product where cid='{$_GET['cid']}'";
$result=mysql_query($sql_email);
while($row=mysql_fetch_array($result))
{
//data fetch
fwrite($file_pointer,"$icode,$stk,$poc,$bocc,$bocu,$type,$design,$d1,$d2,$ht1,$ht2,$list,$cid\n");

}
fwrite($file_pointer,$string);
fclose($file_pointer);
header("location:download.php?file=$cname.txt");

Please suggest me how to proceed,

Thanks in advance
Raj

so_is_this
10-25-2006, 09:42 AM
Whether Excel will honor the use of "mixed" delimiters will be proven out only by testing it. Strictly speaking, "CSV" means comma-separated-variable. So, try it and see. As for the tab character:
$tab = "\t";

NogDog
10-25-2006, 10:01 AM
Not sure if this is what you're after, but a CSV field can have commas within it, as long as the entire field is quoted. For example, this would be treated as 3 fields, not 5, as the quoted section would be treated as one field:

field1,field2,"this,is,field3"

raj_2006
10-25-2006, 10:40 AM
Hi

Thanks for your posts.The fact is that there is no way to give input of "" around the record while inserting into the mysql db.So the data will be remain in the db in this way always:


Name age products

A 34 B,C,D
E 35 F,G,H

not in this way


Name age products

A 34 "B,C,D"
E 35 "F,G,H"

So else using the double quotes whats the other way to use tab as delimiter instead of commas.

If i use "\t" then how i will use it?

so_is_this
10-25-2006, 11:03 AM
The fact is that there is no way to give input of "" around the record while inserting into the mysql db.
You don't necessarily have to put it in the database that way -- just do it when you're converting to a CSV file format. However, yes, there is a way to put it into the database that way:
$sql = "Insert Into MyTable fld1, fld2, fld3 Values('{$val1}', {$val2}, '{$val3},{$val4},{$val5}');";

NogDog
10-25-2006, 11:41 AM
fputcsv() will automatically handle the quoting of fields which contain commas.

$result = mysql_query($sql); // where $sql is your query
$fh = fopen('output.csv', 'w')
while($row = mysql_fetch_array($result))
{
fputcsv($fh, $row);
}
fclose($fh)
// view the results:
$output = file_get_contents('output.csv');
echo "<pre>$output</pre>\n";

so_is_this
10-25-2006, 12:23 PM
fputcsv() will automatically handle the quoting of fields which contain commas.
Hey! When did PHP acquire an fputcsv() function? I wrote my own because the PHP manual I have says there is no equivalent fputcsv() function for the fgetcsv() function. ;)

NogDog
10-25-2006, 12:33 PM
I just double-checked, and fputcsv() is PHP5, so you might still want to stick with your custom function if you want your code to be more portable.

NogDog
10-25-2006, 12:35 PM
Note to "raj", you can use implode() to easily create a tab-delimited file:

$fh = fopen('tabbed.txt', 'w');
while($row = mysql_fetch_array($queryResult))
{
fwrite($fh, implode("\t", $row));
}
fclose($fh);

so_is_this
10-25-2006, 12:39 PM
That brings to mind a question I've had about PHP (which PHP is only a recent, as of two years, addition to my programming languages repertoire). Please forgive a short hijacking. But...

Can I test for the existence of a particular intrinsic function and only instantiate my custom function in the case that the intrinsic function does not exist?

NogDog
10-25-2006, 01:00 PM
That brings to mind a question I've had about PHP (which PHP is only a recent, as of two years, addition to my programming languages repertoire). Please forgive a short hijacking. But...

Can I test for the existence of a particular intrinsic function and only instantiate my custom function in the case that the intrinsic function does not exist?

if(!function_exists("some_function"))
{
function some_function()
{
// function definition
}
}

Note that in this case, since you are conditionally defining a function, this definition must appear in your code before you call the function.

so_is_this
10-25-2006, 01:04 PM
ˇGracias tanto!

ˇSalud!

NogDog
10-25-2006, 01:06 PM
De nada.