FredFogg Posted October 17, 2010 Share Posted October 17, 2010 I have a MySQL database with each record of a person who has registered for an event, I am displaying the information on a web page for a user, but he wants to be able to print out all the records in alphabetical order by last name, first name later on so he will have a hard copy of each person who has registered at the table when they arrive. How can I write each record to a Txt file that he can print out later that will be formated with the record contents along with each fields definition (Ex. Last Name - Smith, First Name - John, etc)? Quote Link to comment https://forums.phpfreaks.com/topic/216053-read-mysql-database-write-out-each-record-to-txt-file/ Share on other sites More sharing options...
ignace Posted October 17, 2010 Share Posted October 17, 2010 Too far fetched to me. If he wants to print it out then simply show him a [print] button that reads out your records and put's it in a table (repeat headers every x records) and implement a print-media stylesheet. <link href="print.css" type="text/css" media="print" rel="stylesheet"> Quote Link to comment https://forums.phpfreaks.com/topic/216053-read-mysql-database-write-out-each-record-to-txt-file/#findComment-1122898 Share on other sites More sharing options...
FredFogg Posted October 17, 2010 Author Share Posted October 17, 2010 I don't want him to have to hit Print for each record, there will several hundred people that have registered. There has to be a way to read each record from the database, write the headers and the record to a txt file through a loop. The later on he can print the txt file. I just don't know how to do it in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/216053-read-mysql-database-write-out-each-record-to-txt-file/#findComment-1122931 Share on other sites More sharing options...
ignace Posted October 17, 2010 Share Posted October 17, 2010 records -- plural Quote Link to comment https://forums.phpfreaks.com/topic/216053-read-mysql-database-write-out-each-record-to-txt-file/#findComment-1122933 Share on other sites More sharing options...
litebearer Posted October 17, 2010 Share Posted October 17, 2010 A simple example... <?PHP include('db.php'); /* change to YOUR database connection */ $query = "SELECT * FROM main ORDER BY lastname, firstname"; /* change to YOUR table name */ $result = mysql_query($query); $output = "Last name, First name\r\n"; while($row= mysql_fetch_array($result)){ $output = $output . $row['lastname'] . ", " . $row['firstname'] . "\r\n"; } $newfile="newfile.txt"; $file = fopen ($newfile, "w"); fwrite($file, $output); fclose ($file); ?> <a href="newfile.txt">Right click to view the list - left click to save the list</a> <?PHP ?> Quote Link to comment https://forums.phpfreaks.com/topic/216053-read-mysql-database-write-out-each-record-to-txt-file/#findComment-1122940 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.