JSHINER Posted September 25, 2007 Share Posted September 25, 2007 Here's some code: function get($db) { return $db->getRow('SELECT field_1, field_2 FROM table WHERE pull = y'); } $page = array(); $page['results'] = get($db); foreach ($page['results'] as $c) { echo $field1, ',http://www.site.com/', field2, '<br>'; } Which will display results as: 00001,http://www.site.com/020202 00002,http://www.site.com/020203 00003,http://www.site.com/020204 I need this info displayed on a .txt file that is updated each time that text file is called up. So if a new result is added, when that text file is called up I need that new listing to appear there. Or every time a new result is added, it updates the text file. Whichever makes the most sense, I'm not sure. How can I go about doing this? Link to comment https://forums.phpfreaks.com/topic/70651-solved-i-need-to-export-query-results-to-a-txt-file/ Share on other sites More sharing options...
jaymc Posted September 25, 2007 Share Posted September 25, 2007 $contents = file_get_contents($textfile); $newdata = $contents."\n$the_new_line_var_here"; file_put_contents($newdata, $textfile) Link to comment https://forums.phpfreaks.com/topic/70651-solved-i-need-to-export-query-results-to-a-txt-file/#findComment-355068 Share on other sites More sharing options...
JSHINER Posted September 25, 2007 Author Share Posted September 25, 2007 I'm not sure I follow ... ? Link to comment https://forums.phpfreaks.com/topic/70651-solved-i-need-to-export-query-results-to-a-txt-file/#findComment-355070 Share on other sites More sharing options...
JSHINER Posted September 25, 2007 Author Share Posted September 25, 2007 file_put_contents is not working. Link to comment https://forums.phpfreaks.com/topic/70651-solved-i-need-to-export-query-results-to-a-txt-file/#findComment-355079 Share on other sites More sharing options...
rarebit Posted September 25, 2007 Share Posted September 25, 2007 You probably haven't got an existing file... see http://us3.php.net/manual/en/function.file-put-contents.php or Here's an append function (if it's any use): function file_append($fn, $s) { $fp = fopen($fn, "a"); // use to append $written = 0; while ($written == 0) // keep trying until lock is free { if (flock($fp, LOCK_EX)) { fwrite($fp, $s); flock($fp, LOCK_UN); $written = 1; } } fclose($fp); return 0; } Link to comment https://forums.phpfreaks.com/topic/70651-solved-i-need-to-export-query-results-to-a-txt-file/#findComment-355081 Share on other sites More sharing options...
JSHINER Posted September 25, 2007 Author Share Posted September 25, 2007 $contents = file_get_contents('file.txt'); $vara = $db->$_GET['vara']; $varb = $db->$_GET['varb']; $newdata = $contents."\n$vara,http://www.site.com/$varb"; file_put_contents('file.txt', $newdata); What is wrong with the above code? It does not write anything to the file. Link to comment https://forums.phpfreaks.com/topic/70651-solved-i-need-to-export-query-results-to-a-txt-file/#findComment-355083 Share on other sites More sharing options...
roopurt18 Posted September 25, 2007 Share Posted September 25, 2007 I need this info displayed on a .txt file that is updated each time that text file is called up. How exactly is this text file "called up?" Through the web browser? Link to comment https://forums.phpfreaks.com/topic/70651-solved-i-need-to-export-query-results-to-a-txt-file/#findComment-355095 Share on other sites More sharing options...
JSHINER Posted September 25, 2007 Author Share Posted September 25, 2007 I have changed my plan of attack. I want to update the text file via the above code now. Link to comment https://forums.phpfreaks.com/topic/70651-solved-i-need-to-export-query-results-to-a-txt-file/#findComment-355097 Share on other sites More sharing options...
jscix Posted September 25, 2007 Share Posted September 25, 2007 $contents = file_get_contents('file.txt'); $vara = $db->$_GET['vara']; $varb = $db->$_GET['varb']; $newdata = $contents."\n$vara,http://www.site.com/$varb"; if (is_writable("file.txt")) { $txtfile = fopen('file.txt',a); fwrite($txtfile, $newdata); fclose($txtfile); } should work ....... Link to comment https://forums.phpfreaks.com/topic/70651-solved-i-need-to-export-query-results-to-a-txt-file/#findComment-355100 Share on other sites More sharing options...
roopurt18 Posted September 25, 2007 Share Posted September 25, 2007 If the .txt file is being called up in a web browser, why not just change the link to the file to a .php script. Then do the following: <?php header("Content-type: text/plain"); $sql = "SELECT ..."; $q = mysql_query($sql); if($q){ while($row = mysql_fetch_assoc($q)){ echo "{$row['col1']},{$row['col2']}"; } } ?> Link to comment https://forums.phpfreaks.com/topic/70651-solved-i-need-to-export-query-results-to-a-txt-file/#findComment-355110 Share on other sites More sharing options...
JSHINER Posted September 25, 2007 Author Share Posted September 25, 2007 Ok now how do I get line breaks in there? So it displays: 0000,g 0001,b 0002,t Link to comment https://forums.phpfreaks.com/topic/70651-solved-i-need-to-export-query-results-to-a-txt-file/#findComment-355118 Share on other sites More sharing options...
rarebit Posted September 25, 2007 Share Posted September 25, 2007 \n Link to comment https://forums.phpfreaks.com/topic/70651-solved-i-need-to-export-query-results-to-a-txt-file/#findComment-355122 Share on other sites More sharing options...
JSHINER Posted September 25, 2007 Author Share Posted September 25, 2007 header("Content-type: text/plain"); echo $row['num'], ',http://www.site.com/listing_view/', $row['id'], '\n'; Displays the results as: 0000,g\n0001,b\n0002,t\n Link to comment https://forums.phpfreaks.com/topic/70651-solved-i-need-to-export-query-results-to-a-txt-file/#findComment-355124 Share on other sites More sharing options...
rarebit Posted September 25, 2007 Share Posted September 25, 2007 Use " instead of ' Link to comment https://forums.phpfreaks.com/topic/70651-solved-i-need-to-export-query-results-to-a-txt-file/#findComment-355126 Share on other sites More sharing options...
roopurt18 Posted September 25, 2007 Share Posted September 25, 2007 You have to place \n inside of double quotes. Link to comment https://forums.phpfreaks.com/topic/70651-solved-i-need-to-export-query-results-to-a-txt-file/#findComment-355127 Share on other sites More sharing options...
JSHINER Posted September 25, 2007 Author Share Posted September 25, 2007 Thanks! Link to comment https://forums.phpfreaks.com/topic/70651-solved-i-need-to-export-query-results-to-a-txt-file/#findComment-355129 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.