/    Sign up×
Community /Pin to ProfileBookmark

tab delimiter in csv file

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:

[code=php]$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,CIDn”);//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,$cidn”);

}
fwrite($file_pointer,$string);
fclose($file_pointer);
header(“location:download.php?file=$cname.txt”);[/code]

Please suggest me how to proceed,

Thanks in advance
Raj

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisOct 25.2006 — 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:
[code=php]$tab = "t";[/code]
Copy linkTweet thisAlerts:
@NogDogOct 25.2006 — 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:
<i>
</i>field1,field2,"this,is,field3"
Copy linkTweet thisAlerts:
@raj_2006authorOct 25.2006 — 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[/QUOTE]


not in this way


Name age products

A 34 "B,C,D"

E 35 "F,G,H"[/QUOTE]


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?
Copy linkTweet thisAlerts:
@so_is_thisOct 25.2006 — The fact is that there is no way to give input of "" around the record while inserting into the mysql db.[/QUOTE]
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:
[code=php]$sql = "Insert Into MyTable fld1, fld2, fld3 Values('{$val1}', {$val2}, '{$val3},{$val4},{$val5}');";[/code]
Copy linkTweet thisAlerts:
@NogDogOct 25.2006 — fputcsv() will automatically handle the quoting of fields which contain commas.
[code=php]
$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";
[/code]
Copy linkTweet thisAlerts:
@so_is_thisOct 25.2006 — fputcsv() will automatically handle the quoting of fields which contain commas.[/QUOTE]
Hey! When did PHP acquire an [B]fputcsv()[/B] function? I wrote my own because the PHP manual I have says there is no equivalent [B]fputcsv()[/B] function for the [B]fgetcsv()[/B] function. ?
Copy linkTweet thisAlerts:
@NogDogOct 25.2006 — 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.
Copy linkTweet thisAlerts:
@NogDogOct 25.2006 — Note to "raj", you can use implode() to easily create a tab-delimited file:
[code=php]
$fh = fopen('tabbed.txt', 'w');
while($row = mysql_fetch_array($queryResult))
{
fwrite($fh, implode("t", $row));
}
fclose($fh);
[/code]
Copy linkTweet thisAlerts:
@so_is_thisOct 25.2006 — 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?
Copy linkTweet thisAlerts:
@NogDogOct 25.2006 — 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?[/QUOTE]

[code=php]
if(!function_exists("some_function"))
{
function some_function()
{
// function definition
}
}
[/code]

Note that in this case, since you are conditionally defining a function, this definition must appear in your code before you call the function.
Copy linkTweet thisAlerts:
@so_is_thisOct 25.2006 — ¡Gracias tanto!

¡Salud!
Copy linkTweet thisAlerts:
@NogDogOct 25.2006 — De nada.
×

Success!

Help @raj_2006 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...