csv problem.. read csv from var
csv problem.. read csv from var
am 16.02.2007 07:52:06 von bedul
i have problem with reading csv.. for i know the example script i get was
========================BASIC SCRIPT=================
$handle = fopen("hasillab.csv", "r");
$data2=$handle;
while($data = fgetcsv($handle, 1000, ",")){
foreach($data as $str)
$data2.="
= ".$str;
}
the result was:
==========================OUTPUT==========================
= A507257
= 3/2/2007
= Hematologi Lengkap,Cholesterol Total,LDL Cholesterol,Trigliserida,HDL
Cholesterol
hasillab.csv contain
A507257,3/2/2007,"Hematologi Lengkap,Cholesterol Total,LDL
Cholesterol,Trigliserida,HDL Cholesterol,Asam Urat,Gula Darah Puasa,Gula
Darah 2 Jam PP,Kreatinin,Ureum,Bilirubin Total,Alkali
Fosfatase,SGOT,SGPT,Urine Lengkap,Feses Rutin,Darah Samar Faeces,VDRL,Anti -
HBs,Total PSA,HBsAg,Anti - HCV Total"
the problem i have is.. how about the csv is a var
========================DATA===============
$csv="
A507257,3/2/2007,\"Hematologi Lengkap,Cholesterol Total,LDL
Cholesterol,Trigliserida,HDL Cholesterol,Asam Urat,Gula Darah Puasa,Gula
Darah 2 Jam PP,Kreatinin,Ureum,Bilirubin Total,Alkali
Fosfatase,SGOT,SGPT,Urine Lengkap,Feses Rutin,Darah Samar Faeces,VDRL,Anti -
HBs,Total PSA,HBsAg,Anti - HCV Total\"";
what should i do to make the ouput like above.
until now.. i try save the var into files then i use basic script to load it
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: csv problem.. read csv from var
am 16.02.2007 07:59:12 von Niel Archer
hi
> the problem i have is.. how about the csv is a var
> ========================DATA===============
> $csv="
> A507257,3/2/2007,\"Hematologi Lengkap,Cholesterol Total,LDL
> Cholesterol,Trigliserida,HDL Cholesterol,Asam Urat,Gula Darah Puasa,Gula
> Darah 2 Jam PP,Kreatinin,Ureum,Bilirubin Total,Alkali
> Fosfatase,SGOT,SGPT,Urine Lengkap,Feses Rutin,Darah Samar Faeces,VDRL,Anti -
> HBs,Total PSA,HBsAg,Anti - HCV Total\"";
>
> what should i do to make the ouput like above.
Use
$arry = explode(',', $csv);
to separate the variable into an array, then process it using foreach as
before
Niel
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: csv problem.. read csv from var
am 16.02.2007 09:03:32 von bedul
----- Original Message -----
From: "Niel Archer"
To:
Sent: Friday, February 16, 2007 1:59 PM
Subject: Re: [PHP-DB] csv problem.. read csv from var
> hi
>
> > the problem i have is.. how about the csv is a var
> > ========================DATA===============
> > $csv="
> > A507257,3/2/2007,\"Hematologi Lengkap,Cholesterol Total,LDL
> > Cholesterol,Trigliserida,HDL Cholesterol,Asam Urat,Gula Darah Puasa,Gula
> > Darah 2 Jam PP,Kreatinin,Ureum,Bilirubin Total,Alkali
> > Fosfatase,SGOT,SGPT,Urine Lengkap,Feses Rutin,Darah Samar
Faeces,VDRL,Anti -
> > HBs,Total PSA,HBsAg,Anti - HCV Total\"";
> >
> > what should i do to make the ouput like above.
>
> Use
>
> $arry = explode(',', $csv);
>
=============================CODE====================
$csv="
A507257,3/2/2007,\"Hematologi Lengkap,Cholesterol Total,LDL
Cholesterol,Trigliserida,HDL Cholesterol,Asam Urat,Gula Darah Puasa,Gula
Darah 2 Jam PP,Kreatinin,Ureum,Bilirubin Total,Alkali
Fosfatase,SGOT,SGPT,Urine Lengkap,Feses Rutin,Darah Samar
Faeces,VDRL,Anti -
HBs,Total PSA,HBsAg,Anti - HCV Total\"";
$arry = explode(',', $csv);
foreach($arry as $val){
$txt.= "=".$val . "
\n";
}
print $txt;
?>
=============================OUTPUT===================
= A507257
=3/2/2007
="Hematologi Lengkap
=Cholesterol Total
=LDL Cholesterol
=Trigliserida
=HDL Cholesterol
=Asam Urat
=Gula Darah Puasa
=Gula Darah 2 Jam PP
=Kreatinin
=Ureum
=Bilirubin Total
=Alkali Fosfatase
=SGOT
=SGPT
=Urine Lengkap
=Feses Rutin
=Darah Samar Faeces
=VDRL
=Anti - HBs
=Total PSA
=HBsAg
=Anti - HCV Total"
> to separate the variable into an array, then process it using foreach as
> before
i'll already try that! fail.. it has tobe return 3 not ..10
thx for the reply
> Niel
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: csv problem.. read csv from var
am 16.02.2007 20:43:50 von Niel Archer
Hi
sorry, I misunderstood your problem, it was not too clear to me as the
code you list shouldn't produce the output you supplied.
try this:
$array = explode(',', $csv);
print "
= " . $array[0] . "\n";
print "
= " . $array[1] . "\n";
for ($i = 2; $i < count($array); ++$i) {
$txt.= "
=" . $array[$i] . "\n";
}
print $txt;
Niel
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: csv problem.. read csv from var
am 17.02.2007 04:03:36 von Martin Alterisio
------=_Part_15765_18459682.1171681416514
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
2007/2/16, bedul :
>
> i have problem with reading csv.. for i know the example script i get was
> ========================BASIC SCRIPT=================
> $handle = fopen("hasillab.csv", "r");
> $data2=$handle;
> while($data = fgetcsv($handle, 1000, ",")){
> foreach($data as $str)
> $data2.="
= ".$str;
>
> }
>
> the result was:
> ==========================OUTPUT==========================
> = A507257
> = 3/2/2007
> = Hematologi Lengkap,Cholesterol Total,LDL Cholesterol,Trigliserida,HDL
> Cholesterol
>
> hasillab.csv contain
> A507257,3/2/2007,"Hematologi Lengkap,Cholesterol Total,LDL
> Cholesterol,Trigliserida,HDL Cholesterol,Asam Urat,Gula Darah Puasa,Gula
> Darah 2 Jam PP,Kreatinin,Ureum,Bilirubin Total,Alkali
> Fosfatase,SGOT,SGPT,Urine Lengkap,Feses Rutin,Darah Samar Faeces,VDRL,Anti
> -
> HBs,Total PSA,HBsAg,Anti - HCV Total"
>
> the problem i have is.. how about the csv is a var
> ========================DATA===============
> $csv="
> A507257,3/2/2007,\"Hematologi Lengkap,Cholesterol Total,LDL
> Cholesterol,Trigliserida,HDL Cholesterol,Asam Urat,Gula Darah Puasa,Gula
> Darah 2 Jam PP,Kreatinin,Ureum,Bilirubin Total,Alkali
> Fosfatase,SGOT,SGPT,Urine Lengkap,Feses Rutin,Darah Samar Faeces,VDRL,Anti
> -
> HBs,Total PSA,HBsAg,Anti - HCV Total\"";
>
> what should i do to make the ouput like above.
>
> until now.. i try save the var into files then i use basic script to load
> it
>
> --
> PHP Database Mailing List ( http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
You can parse the csv yourself. So, lets cast some regexp magic into the
emptyness of this mailing thread:
preg_match_all('/(?<=^|,)("(.|\n)*(?
PREG_SET_ORDER);
$rows = array();
$fields = array();
foreach ($matches as $match) {
$field = $match[1];
if ($field != '' && $field != '"' && $field[0] == '"' &&
substr($field, -1) == '"') {
$field = substr($field, 1, -1);
$field = str_replace('""', '"', $field);
}
$fields[] = $field;
if ($match[3] == '') {
$rows[] = $fields;
$fields = array();
}
}
$rows variable will be filled with an array for each row in a CSV format
complaint string, using line breaks as row separators and commas as field
separators.
If you wonder about the mystic forces that powers this regexp, I'll be more
than willing to explain them throughly, if time becomes available. If it
doesn't work (I tested it againts all the use cases I could think of, but...
murphy's law applies) go blame someone else... =P
----
Also, if you're working on PHP5, you may bring up the new, mostly unused,
spells in your PHP spell book, available to all those who have reached level
5 in PHP wizardry. Just copy & paste (that skill you have probably adquired
in your early days as an apprentice) the code from the PHP manual included
as an example of custom stream wrappers:
http://www.php.net/stream_wrapper_register
With the VariableStream class registered as a stream wrapper, you can
magically use global variables as a streams. Therefore something like this
will work:
$csv="...etc...";
$handle = fopen("var://csv", "r");
.... etc ...
------=_Part_15765_18459682.1171681416514--
Re: csv problem.. read csv from var
am 22.02.2007 10:48:16 von bedul
nope.. u don't give me wrong code
----- Original Message -----
From: "Niel Archer"
To:
Sent: Saturday, February 17, 2007 2:43 AM
Subject: Re: [PHP-DB] csv problem.. read csv from var
> Hi
>
> sorry, I misunderstood your problem, it was not too clear to me as the
> code you list shouldn't produce the output you supplied.
>
> try this:
>
> $array = explode(',', $csv);
>
> print "
= " . $array[0] . "\n";
> print "
= " . $array[1] . "\n";
>
> for ($i = 2; $i < count($array); ++$i) {
> $txt.= "
=" . $array[$i] . "\n";
> }
>
> print $txt;
>
>
> Niel
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
the code u mention before i tried to my code and it's not right..
thx for your support
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php