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
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

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

Fine, I am not saying it doesn't do what you expect, but you have a broken <br /> tag in there, and you are opening and closing PHP tags (<?php & ?>) far too many times really.

 

Have you tried using the code to create your text file?

Link to comment
Share on other sites

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 />";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.