Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export the result into a CSV 1

Status
Not open for further replies.

aas1611

Programmer
Dec 14, 2001
184
DE
Suppose I already have the following result appeared on my page:

No Account Note
1 11 A
2 22 B
3 33 C
4 44 D

Then, I want to export this to a txt file with CSV format, so it would appear like the following in the file:
1,11,A
2,22,B
3,33,C
4,44,D

How to do this?
 
Can you explain a little more about this functon?

I've read the explanation and the sample codes, but it still doesn't do what I want it to do or I still don't get it quite right.

The query result already appears on my webpage. Then I want to send (export) this result to a txt file with CSV format.

Help? or any other solution?
 
Can we see your code? It's hard to tell you what you are doing incorrectly without it.

Ken
 
## Here is my code that gives result to my webpage. I haven't done anything to export it to a txt file.

<?

include 'header.htm';
include 'db_connection.php';

$query = "SELECT sperrgrund, blz, kontonr, beschreibung FROM sperr001";
$result = mysql_query($query);
$num = mysql_numrows($result);


?>

<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Sperrgrund</font></th>
<th><font face="Arial, Helvetica, sans-serif">Bankleitzahl</font></th>
<th><font face="Arial, Helvetica, sans-serif">Kontonummer</font></th>
<th><font face="Arial, Helvetica, sans-serif">Beschreibung</font></th>
</tr>

<?

$i = 0;

while($i < $num) {
$sperrgrund = mysql_result($result,$i,"sperrgrund");
$blz = mysql_result($result,$i,"blz");
$kontonr = mysql_result($result,$i,"kontonr");
$beschreibung = mysql_result($result,$i,"beschreibung");

?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo $sperrgrund; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $blz; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $kontonr; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $beschreibung; ?></font></td>
</tr>
<?
$i++;
}

echo "</table>";

mysql_close($conn);

?>

 
Thanks for posting your code, now we can make some progress. I'm also going to modify your code to make it shorter.
Code:
<?

include 'header.htm';
include 'db_connection.php';

// open output file
$fp = fopen('test.csv','w');

$query = "SELECT sperrgrund, blz, kontonr, beschreibung FROM sperr001";
$result = mysql_query($query);
// the following line is not needed anymore
// $num = mysql_numrows($result);


?>

<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Sperrgrund</font></th>
<th><font face="Arial, Helvetica, sans-serif">Bankleitzahl</font></th>
<th><font face="Arial, Helvetica, sans-serif">Kontonummer</font></th>
<th><font face="Arial, Helvetica, sans-serif">Beschreibung</font></th>
</tr>

<?
// the following line is not needed anymore
// $i = 0;

while($rw = mysql_fetch_assoc($result)) {
// don't need the following four lines if you use the above method of getting each row
//    $sperrgrund = mysql_result($result,$i,"sperrgrund");
//    $blz = mysql_result($result,$i,"blz");
//    $kontonr = mysql_result($result,$i,"kontonr");
//    $beschreibung = mysql_result($result,$i,"beschreibung");
//
// write each row to file with values seperated with commas
//
   fwrite($fp, implode(',',$rw)."\n");
?>
    <tr>
    <td><font face="Arial, Helvetica, sans-serif"><? echo $rw['sperrgrund']; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo $rw['blz']; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo $rw['kontonr']; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo $rw['beschreibung; ?></font></td>
    </tr>']
<?
//    $i++;    
}

echo "</table>";

mysql_close($conn);
//
//  close file
//
    fclose($fp);
?>

Using mysql_fetch_assoc() will put each row into an associative array. I use implode() to write each row with the values seperated with commas.

Ken
 
It works, wonderful!! THANKS!!

Now let's say a user want to export this to her local computer. How can she choose her own directory? Should there be an input form and/or function ? How would it be?
 
again, follow the thread I posted you... in that post you gonna see how to make the file get downloaded.

Cheers.

Chacal, Inc.
 
Alrighty, I will give it a look.

Thank you both for your help!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top