Mombu the Php Forum

Go Back   Mombu the Php Forum > Php > CSV file with PHP
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 1st July 19:46
lists
External User
 
Posts: 1
Default CSV file with PHP



Hi all,

Does anybody knows how to make a CSV (comma separated values) file with
PHP based on results fetched from MySQL? I need it to import it with
Microsoft Outlook Express.

Thanks in advanced,

Cesar Aracena
www.icaam.com.ar
  Reply With Quote


 


2 1st July 19:46
php-general
External User
 
Posts: 1
Default CSV file with PHP



csv file format as follows:
value,"value\" with quotes",value with space,value<CR>
with optional column headings on the first line. the <CR> is a actual
carriage return "\n".

I'm not sure what you mean by importing it with OE.


Curt.
  Reply With Quote


 


3 1st July 19:46
dan
External User
 
Posts: 1
Default CSV file with PHP


You could do something like:

$result = mysql_query($query) or die(mysql_error()); // don't display
errors on production machines
$first = TRUE;
while ($line = mysql_fetch_array($result))
{
if ($first)
{
foreach ($line as $key => $value)
{
if ($first)
{ fwrite($file_handle, $key); $first = FALSE; }
else
{ fwrite($file_handle, ", $key"); }
}
fwrite ($file_handle, "\n");
}
$number1 = TRUE;
foreach ($line as $value)
{
if ($number1)
{ fwrite ($file_hande, $value); $number1 = FALSE; }
else
{ fwiret ($file_hande, ", {$value}"; };
}
fwrite ($file_handle, "\n");
}

That is pseudo code though. (i.e. I don't have time to test it and
debug it and make it nice and neat and readable and conform to your
specifications -- but it should give you a good idea of how to start).

-Dan
  Reply With Quote
4 1st July 19:46
php-general
External User
 
Posts: 1
Default CSV file with PHP


Here is a little more efficient code:

if ($row = mysql_fetch_assoc($result)) {
$line = '';
foreach ($row as $key => $value) {
$line .= "$key,";
}
fwrite($file_handle, substr($line, 0, -1)."\n");

do {

$line = '';
foreach ($row as $value) {
if (strpos($value, '"') ) {
$line .= sprintf('"%s",', str_replace('"', '\\"', $value));
} else {
$line .= "$value,";
}
}
fwrite($file_handle, substr($line, 0, -1)."\n");

} while ($line = mysql_fetch_assoc($result));
}
  Reply With Quote
5 1st July 19:48
list-php-1
External User
 
Posts: 1
Default CSV file with PHP


: On Fri, 10 Oct 2003 18:38:04 -0300, Cesar Aracena <lists@icaam.com.ar> : wrote:
: >
: >Does anybody knows how to make a CSV (comma separated values) file
: >with PHP based on results fetched from MySQL? I need it to import it
: >with Microsoft Outlook Express.
:
: csv file format as follows: :
: value,"value\" with quotes",value with space,value<CR>

What about values with commas?
  Reply With Quote
6 1st July 19:49
php-general
External User
 
Posts: 1
Default CSV file with PHP


doh!

"value, with comma"


Curt
--
"My PHP key is worn out"

PHP List stats since 1997:
http://zirzow.dyndns.org/html/mlists/
  Reply With Quote
Reply


Thread Tools
Display Modes




666