RSS Forums RSS
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 2526 | Replies: 8 | Solved
Reply
Join Date: May 2007
Posts: 61
Reputation: dudegio is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dudegio dudegio is offline Offline
Junior Poster in Training

Generate text document from Php

  #1  
May 10th, 2007
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: Bucharest, RO
Posts: 67
Reputation: johny_d is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
johny_d's Avatar
johny_d johny_d is offline Offline
Junior Poster in Training

Re: Generate text document from Php

  #2  
May 10th, 2007
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)
Last edited by johny_d : May 10th, 2007 at 9:01 am.
Reply With Quote  
Join Date: May 2007
Posts: 61
Reputation: dudegio is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dudegio dudegio is offline Offline
Junior Poster in Training

Re: Generate text document from Php

  #3  
May 10th, 2007
Thanks for your help! I appreciate it very much.
Reply With Quote  
Join Date: May 2007
Location: Bucharest, RO
Posts: 67
Reputation: johny_d is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
johny_d's Avatar
johny_d johny_d is offline Offline
Junior Poster in Training

Re: Generate text document from Php

  #4  
May 11th, 2007
You're welcome!
Reply With Quote  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: Generate text document from Php

  #5  
May 11th, 2007
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....
Last edited by dr4g : May 11th, 2007 at 2:25 pm.
GardCMS :: Open Source CMS :: Gardcms.org
Reply With Quote  
Join Date: Dec 2006
Location: syria
Posts: 158
Reputation: w_3rabi is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
w_3rabi's Avatar
w_3rabi w_3rabi is offline Offline
Junior Poster

Re: Generate text document from Php

  #6  
May 12th, 2007
.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
programming is an art ,only for those who can understand it.
- th3 php wr3nch -
Reply With Quote  
Join Date: Sep 2007
Posts: 11
Reputation: kye is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
kye kye is offline Offline
Newbie Poster

Re: Generate text document from Php

  #7  
Mar 28th, 2008
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! ?
Reply With Quote  
Join Date: Mar 2007
Location: georgia
Posts: 55
Reputation: hunkychop is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
hunkychop's Avatar
hunkychop hunkychop is offline Offline
Junior Poster in Training

Re: Generate text document from Php

  #8  
Mar 28th, 2008
http://php.net

there is a search box that will search all of the functions available, documentation, and more in php.
toast
Reply With Quote  
Join Date: Sep 2007
Posts: 11
Reputation: kye is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
kye kye is offline Offline
Newbie Poster

Re: Generate text document from Php

  #9  
Mar 29th, 2008
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
All times are GMT -4. The time now is 12:09 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC