Hello everyone! Im new to the language Php and i need your help. Please help me on how to generate a text document or an *.xls file using Php. Data to be generated are taken from the database. Thanks and hoping for your response.

Recommended Answers

All 9 Replies

You can find it in the php manual if you do a little search :)
Here is that code adjusted to what you need:

<?php
 
/// here is your database connection
 
/// than the query
$result = mysql_query ("select field_1, field_2, field_3 from table") or die (mysql_error());
 
$filename = $_SERVER['DOCUMENT_ROOT'].'/test.xls'; //// or whatever.txt
 
if (!$handle = fopen($filename, 'a')) { 
        print "Cannot open file ($filename)"; 
        exit; 
   } 
// Let's make sure the file exists and is writable first. 
if (is_writable($filename)) { 
 
while ($row = mysql_fetch_row($result)) {
 $row_content = implode ("\t",$row)."\r\n";
   // Write $row_content to our opened file. 
   if (!fwrite($handle, $row_content)) { 
       print "Cannot write to file ($filename)"; 
       exit; 
   } 
   }
 
   print "Success, content wrote to file ($filename)"; 
 
   fclose($handle); 
 
} else { 
   print "The file $filename is not writable"; 
} 
 
?>

Try to do the basics first (like reading the manual) ;)

Thanks for your help! I appreciate it very much.

You're welcome! :)

Just a Tip for you, as you're new to PHP.
Once you've written to the txt file and want to get all the info from it again.
You can use the file_get_contents() function to store the file's contents into a string.
Example:

$str = file_get_contents("file.txt");
echo $str;
 
Drag....

.xls ???
do you mean an ms excel file or something like that???
there is a classes ready to handle the .xls files if u want to get the data of an excel file.
if u r just using this extension to save text then u could do the ways above
anyway just wanted to make this thing clear

Just what I've been looking for (for the past 4+ hours!!!) in the PHP Manual! He says "just look in the manual" -- WHERE in the manual? I've been all thru streams, wrappers, all kinds of stuff!!! The code you have here looks like JUST what I need (take mySQL query result and put into a text file) - and I SINCERELY thank you for it! BUT, where in the dang manual should I have looked! ?

http://php.net

there is a search box that will search all of the functions available, documentation, and more in php.

Yes, there sure is. And that's what I was using. Unfortunately unless you know what to search for (like - now I know I should have been looking for the term "implode" - in which case I wouldn't have needed to use the search anyway!) and you are just putting in things like "output as text file", etc., the right result doesn't particularly come up! If I there was a keywords search instead of just a rudimentary "search for words including" type thing like is the case, I would have been able to find the function without needing to know what it is called.

You can find it in the php manual if you do a little search :)
Here is that code adjusted to what you need:

<?php
 
/// here is your database connection
 
/// than the query
$result = mysql_query ("select field_1, field_2, field_3 from table") or die (mysql_error());
 
$filename = $_SERVER['DOCUMENT_ROOT'].'/test.xls'; //// or whatever.txt
 
if (!$handle = fopen($filename, 'a')) { 
        print "Cannot open file ($filename)"; 
        exit; 
   } 
// Let's make sure the file exists and is writable first. 
if (is_writable($filename)) { 
 
while ($row = mysql_fetch_row($result)) {
 $row_content = implode ("\t",$row)."\r\n";
   // Write $row_content to our opened file. 
   if (!fwrite($handle, $row_content)) { 
       print "Cannot write to file ($filename)"; 
       exit; 
   } 
   }
 
   print "Success, content wrote to file ($filename)"; 
 
   fclose($handle); 
 
} else { 
   print "The file $filename is not writable"; 
} 
 
?>

Try to do the basics first (like reading the manual) ;)

Can you help me in my problem? Regarding in printing a document into the printer using php?
I have this idea, i have a file or document then i want to send it to the printer so that it could be printed out into a hard copy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.