Jump to content

Output data to text file


mycow

Recommended Posts

I have made a simple script that checks two tables for some conditions and outputs on the browser the results. The code is like this

<?php do { ?>
<?php echo $row_Recordset1['a1']; ?>;<?php echo $row_Recordset1['w1']; ?>;br />
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

I want that output to be able to save it as a text file and if possible to be able to change the encoding of the file as i use UTF-8 and the file need to be ANSI.

 

Thank you for any help.

Link to comment
https://forums.phpfreaks.com/topic/139987-output-data-to-text-file/
Share on other sites

Sorry... but your code looks really bad, it doesn't even look as if the HTML is correct.

 

<?php do {
     echo ($row_Recordset1['a1'] .';'. $row_Recordset1['w1'] .'<br />');
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

 

If anything that is more understandable.

 

Look here for the code to write to a text file... http://www.tizag.com/phpT/filewrite.php

yes .. your code may work fine but there is no point of putting php tag for each line. Anyway, I think the following will work

<?php
$forwrite="";
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)){
     $forwrite .= $row_Recordset1['a1'].";".$row_Recordset1['w1']."<br />";
}
$filename = "myfile.txt";
if (!$handle = fopen($filename, 'w')) {
      echo "Cannot open file ($filename)";
      exit;
}
if (fwrite($handle, $forwrite) === FALSE) {
      echo "Cannot write to file ($filename)";
      exit;
}
fclose($handle);
?>

well, if you dont put a second space in your code, the only alternative i can think is the data from the data base has that extra space. try trimming everytime you get anything from database, like below ....

 

while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)){
     $forwrite .= trim($row_Recordset1['a1']).";".trim($row_Recordset1['w1'])."<br />";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.