mycow Posted January 8, 2009 Share Posted January 8, 2009 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. Quote Link to comment Share on other sites More sharing options...
ILMV Posted January 8, 2009 Share Posted January 8, 2009 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 Quote Link to comment Share on other sites More sharing options...
mycow Posted January 8, 2009 Author Share Posted January 8, 2009 the code i have outputs something like 000000;10.0.0.0;10;27 and so on... The code works fine Quote Link to comment Share on other sites More sharing options...
abdfahim Posted January 8, 2009 Share Posted January 8, 2009 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); ?> Quote Link to comment Share on other sites More sharing options...
ILMV Posted January 8, 2009 Share Posted January 8, 2009 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? Quote Link to comment Share on other sites More sharing options...
mycow Posted January 8, 2009 Author Share Posted January 8, 2009 the tags were open due to some changes i did ... i had other stuff in there and forgot the erase the tags Quote Link to comment Share on other sites More sharing options...
abdfahim Posted January 8, 2009 Share Posted January 8, 2009 @ILMV .. your question was for me? Anyway .. yup, my code should write a text file. @mycow .. use "\n" or "\r\n" instead of "<br>" if you want a line break in your text file. Quote Link to comment Share on other sites More sharing options...
mycow Posted January 8, 2009 Author Share Posted January 8, 2009 @abdbuet works fine thank you but in some cases it gives me a space. I have allready one in there and look bad with the second. Do you have anything in mind? Thank you again. Quote Link to comment Share on other sites More sharing options...
robb73 Posted January 8, 2009 Share Posted January 8, 2009 @mycow .. use "\n" or "\r\n" instead of "<br>" if you want a line break in your text file. Personally I'd use PHP_EOL Quote Link to comment Share on other sites More sharing options...
abdfahim Posted January 8, 2009 Share Posted January 8, 2009 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 />"; } Quote Link to comment Share on other sites More sharing options...
ILMV Posted January 8, 2009 Share Posted January 8, 2009 @abdbuet No my question wasn't pointing at you, I posted that one at the same time you posted Quote Link to comment Share on other sites More sharing options...
abdfahim Posted January 8, 2009 Share Posted January 8, 2009 @ILMV .. yup, I guessed so .. that's y i asked the question [] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.