DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   Generate text document from Php (http://www.daniweb.com/forums/thread77908.html)

dudegio May 10th, 2007 4:29 am
Generate text document from Php
 
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.

johny_d May 10th, 2007 7:57 am
Re: Generate text document from Php
 
You can find it in the php manual if you do a little search :)
Here is that code adjusted to what you need:
[php]
<?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";
}

?>

[/php]

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

dudegio May 10th, 2007 8:28 pm
Re: Generate text document from Php
 
Thanks for your help! I appreciate it very much.

johny_d May 11th, 2007 12:28 pm
Re: Generate text document from Php
 
You're welcome! :)

dr4g May 11th, 2007 1:25 pm
Re: Generate text document from Php
 
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....

w_3rabi May 12th, 2007 6:14 am
Re: Generate text document from Php
 
.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

kye Mar 28th, 2008 2:06 pm
Re: Generate text document from Php
 
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! ?

hunkychop Mar 28th, 2008 4:40 pm
Re: Generate text document from Php
 
http://php.net

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

kye Mar 29th, 2008 11:55 am
Re: Generate text document from 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.


All times are GMT -4. The time now is 10:49 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC