Array to csv or excel in php
--0015174be42edd836e0484972a29
Content-Type: text/plain; charset=ISO-8859-1
hallo there everyone..
i got an array from my database
Help with Code Tags<http://www.daniweb.com/forums/misc-explaincode.html?TB_iframe=true&height=400&width=680>
*PHP Syntax* (Toggle Plain Text<http://www.daniweb.com/forums/post1194347.html#>
)
1. $save=split("[|;]",$listOfItems);
and what i want i s after making some changes to the attributes on the array
above to export them on an csv or excel format
but directly as a message to the browser ..
i dont want it to be saved on the server ...
what i cant understand from the examples i found on the net ..
is how to handle the files and which are created cause
i just have the array in a php file nothing more...
another thing i have in mind is to export from the ldap server the files
directly but seems to me as the wrong way to do it
thanks
--0015174be42edd836e0484972a29--
Re: Array to csv or excel in php
On Mon, Apr 19, 2010 at 9:45 AM, Manolis Vlachakis
<vlachakis.manolis [at] gmail.com> wrote:
> hallo there everyone..
> i got an array from my database
> Help with Code Tags<http://www.daniweb.com/forums/misc-explaincode.html?T=
B_iframe=3Dtrue&height=3D400&width=3D680>
> *PHP Syntax* (Toggle Plain Text<http://www.daniweb.com/forums/post1194347=
..html#>
> )
>
>
> =C2=A0 1. $save=3Dsplit("[|;]",$listOfItems);
>
>
> and what i want i s after making some changes to the attributes on the ar=
ray
> above to export them on an csv or excel format
> but directly as a message to the browser ..
> i dont want it to be saved on the server ...
> what i cant understand from the examples i found on the net ..
> is how to handle the files and which are created cause
> i just have the array in a php file nothing more...
>
>
> another thing i have in mind is to export from the ldap server the files
> directly but seems to me as the wrong way to do it
>
> thanks
>
Often when outputting csv, I usually do something like this:
<?php
$fp =3D fopen('php://output', 'w') or die('Could not open stream');
foreach ($data as $row) {
// Assumes that $row will be an array.
// Manipulate the data in $row if necessary.
fputcsv($fp, $row);
}
?>
So far, it has worked pretty well and is much faster than any other
way I have found to output the CSV data by iterating through the
arrays manually.
Andrew
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Array to csv or excel in php
On 19 April 2010 17:00, Andrew Ballard <aballard [at] gmail.com> wrote:
> On Mon, Apr 19, 2010 at 9:45 AM, Manolis Vlachakis
> <vlachakis.manolis [at] gmail.com> wrote:
>> hallo there everyone..
>> i got an array from my database
>> Help with Code Tags<http://www.daniweb.com/forums/misc-explaincode.html?=
TB_iframe=3Dtrue&height=3D400&width=3D680>
>> *PHP Syntax* (Toggle Plain Text<http://www.daniweb.com/forums/post119434=
7.html#>
>> )
>>
>>
>> =C2=A0 1. $save=3Dsplit("[|;]",$listOfItems);
>>
>>
>> and what i want i s after making some changes to the attributes on the a=
rray
>> above to export them on an csv or excel format
>> but directly as a message to the browser ..
>> i dont want it to be saved on the server ...
>> what i cant understand from the examples i found on the net ..
>> is how to handle the files and which are created cause
>> i just have the array in a php file nothing more...
>>
>>
>> another thing i have in mind is to export from the ldap server the files
>> directly but seems to me as the wrong way to do it
>>
>> thanks
>>
>
> Often when outputting csv, I usually do something like this:
>
> <?php
>
> $fp =3D fopen('php://output', 'w') or die('Could not open stream');
>
> foreach ($data as $row) {
> =C2=A0 =C2=A0// Assumes that $row will be an array.
> =C2=A0 =C2=A0// Manipulate the data in $row if necessary.
> =C2=A0 =C2=A0fputcsv($fp, $row);
> }
>
> ?>
An interesting idea. I'd do:
echo implode(',', $row);
regards
Peter
--
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
Flickr: http://www.flickr.com/photos/fake51
BeWelcome: Fake51
Couchsurfing: Fake51
</hype>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Array to csv or excel in php
On Mon, Apr 19, 2010 at 11:14 AM, Peter Lind <peter.e.lind [at] gmail.com> wrote=
:
> On 19 April 2010 17:00, Andrew Ballard <aballard [at] gmail.com> wrote:
>> On Mon, Apr 19, 2010 at 9:45 AM, Manolis Vlachakis
>>> 1. $save=3Dsplit("[|;]",$listOfItems);
>>>
>>> and what i want i s after making some changes to the attributes on the =
array
>>> above to export them on an csv or excel format
>>> but directly as a message to the browser ..
>>> i dont want it to be saved on the server ...
>>>
>> Often when outputting csv, I usually do something like this:
>>
>> <?php
>>
>> $fp =3D fopen('php://output', 'w') or die('Could not open stream');
>>
>> foreach ($data as $row) {
>> =C2=A0 =C2=A0// Assumes that $row will be an array.
>> =C2=A0 =C2=A0// Manipulate the data in $row if necessary.
>> =C2=A0 =C2=A0fputcsv($fp, $row);
>> }
>>
>> ?>
>
> An interesting idea. I'd do:
>
> echo implode(',', $row);
>
If it's very simple data that works, but it doesn't allow for the
optional enclosure characters that fputcsv() uses in cases where a
data element includes the column and/or row delimiter characters. I
had originally written something using an array_map callback that did
the optional enclosures as needed and then used echo implode() as you
suggest, but found the solution I posted was shorter and faster. YMMV
Andrew
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Array to csv or excel in php
On 19 April 2010 17:40, Andrew Ballard <aballard [at] gmail.com> wrote:
> On Mon, Apr 19, 2010 at 11:14 AM, Peter Lind <peter.e.lind [at] gmail.com> wro=
te:
>> On 19 April 2010 17:00, Andrew Ballard <aballard [at] gmail.com> wrote:
>>> On Mon, Apr 19, 2010 at 9:45 AM, Manolis Vlachakis
>>>> =C2=A0 1. $save=3Dsplit("[|;]",$listOfItems);
>>>>
>>>> and what i want i s after making some changes to the attributes on the=
array
>>>> above to export them on an csv or excel format
>>>> but directly as a message to the browser ..
>>>> i dont want it to be saved on the server ...
>>>>
>>> Often when outputting csv, I usually do something like this:
>>>
>>> <?php
>>>
>>> $fp =3D fopen('php://output', 'w') or die('Could not open stream');
>>>
>>> foreach ($data as $row) {
>>> =C2=A0 =C2=A0// Assumes that $row will be an array.
>>> =C2=A0 =C2=A0// Manipulate the data in $row if necessary.
>>> =C2=A0 =C2=A0fputcsv($fp, $row);
>>> }
>>>
>>> ?>
>>
>> An interesting idea. I'd do:
>>
>> echo implode(',', $row);
>>
>
> If it's very simple data that works, but it doesn't allow for the
> optional enclosure characters that fputcsv() uses in cases where a
> data element includes the column and/or row delimiter characters. I
> had originally written something using an array_map callback that did
> the optional enclosures as needed and then used echo implode() as you
> suggest, but found the solution I posted was shorter and faster. YMMV
>
> Andrew
>
Yeah, was considering that point as well. I'd use the echo if the
array values are getting modified anyway. Otherwise your solution is
probably simpler.
Regards
Peter
--
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
Flickr: http://www.flickr.com/photos/fake51
BeWelcome: Fake51
Couchsurfing: Fake51
</hype>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Array to csv or excel in php
For non-simple data I have been using PEAR's File_CSV package. It's
proven itself very useful in
regards to not having to determine in my own code whether something
needs to be quoted etc etc - especially if the output CSV needs to be
wholly RFC 4180 compliant.
The documentation of it is rather minimal - at the moment you are
dependant on the test/example files and the API docs but grokking how
to use it is rather easy.
k.
On Mon, Apr 19, 2010 at 4:40 PM, Andrew Ballard <aballard [at] gmail.com> wrote:
> On Mon, Apr 19, 2010 at 11:14 AM, Peter Lind <peter.e.lind [at] gmail.com> wro=
te:
>> On 19 April 2010 17:00, Andrew Ballard <aballard [at] gmail.com> wrote:
>>> On Mon, Apr 19, 2010 at 9:45 AM, Manolis Vlachakis
>>>> =A0 1. $save=3Dsplit("[|;]",$listOfItems);
>>>>
>>>> and what i want i s after making some changes to the attributes on the=
array
>>>> above to export them on an csv or excel format
>>>> but directly as a message to the browser ..
>>>> i dont want it to be saved on the server ...
>>>>
>>> Often when outputting csv, I usually do something like this:
>>>
>>> <?php
>>>
>>> $fp =3D fopen('php://output', 'w') or die('Could not open stream');
>>>
>>> foreach ($data as $row) {
>>> =A0 =A0// Assumes that $row will be an array.
>>> =A0 =A0// Manipulate the data in $row if necessary.
>>> =A0 =A0fputcsv($fp, $row);
>>> }
>>>
>>> ?>
>>
>> An interesting idea. I'd do:
>>
>> echo implode(',', $row);
>>
>
> If it's very simple data that works, but it doesn't allow for the
> optional enclosure characters that fputcsv() uses in cases where a
> data element includes the column and/or row delimiter characters. I
> had originally written something using an array_map callback that did
> the optional enclosures as needed and then used echo implode() as you
> suggest, but found the solution I posted was shorter and faster. YMMV
>
> Andrew
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
http://blogs.linux.ie/kenguest/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Array to csv or excel in php
[snip] to export them on an csv or excel format[/snip]
Stupid browser tricks....
http://www.evolt.org/node/26896
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php