How do u write to a text file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jimeee_1999
Forum Newbie
Posts: 4
Joined: Tue May 28, 2002 8:33 am

How do u write to a text file?

Post by Jimeee_1999 »

Dear Helper:

I was wondering if anyone could help me with a little problem I have. I am a Gr. 12 student at Holy Cross Secondary and I'm doing a school project... I've been working on this project for a while. I need a form to have a person input scores for the games which are played at our school. This I've already managed to have finished. My biggest problem is that I need the information inputted by user, stored in the variables to be outputted onto a file eg. Sport.html or Sport.txt. I've seen the include function used but im not sure how to write to a file. The file must also keep all it's old entries and evertime the new information is entered it should go at the top. I hope I've made myself clear enough about this problem. If not I'm be more then happy to clarify.

Anyways if anyone could please help I'd be eternally greatful!

Thanks for your time
Jim

P.S. This is the code where it writes to the sport.html or sport.txt file.

<?if ($user == "user" and $pass == "pass" and
$team == true and
$HCS == true and
$OTS == true and
$information == true)
{

CODE TO WRITE TO TEXT FILE GOES HERE

?>

<?
}else{
?>
You have either entered the incorrect login/password, or have not filled in one of the form fields.
<?
}
?>
User avatar
enygma
Site Admin
Posts: 175
Joined: Fri Apr 19, 2002 8:29 am
Location: Dallas, Tx

Post by enygma »

fopen/fwrite/fclose are your best friends.

check them out in the manual.
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

This will append to the end of a file not the beginning. Maybe someone else can tell you how to do that. I would probably write a script to go and re-organize the list in the file if I had to have the appended items at the top. Hope I helped,
John

Code: Select all

&lt;?php           
//Open File to append                                                     
$new_file=fopen("file_name.txt","a");   // use the letter "a" to append
if (! $new_file)
    die("new_file did not open");

fwrite($var_name,"put text/labels in quotes" or put more vars);
fwrite( do this as many times as you like);

//Close File
fclose($new_file);
?&gt;
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

<?
//EDIT THESE
$filename = "nameoffile.txt";
//STOP EDITING

if ($user == "user" and $pass == "pass" and 
$team == true and 
$HCS == true and 
$OTS == true and 
$information == true) 
&#123; 

$openfile = fopen("$filename", "a");
$what_to_write = "$team"."|"."$HCS"."|"."$OTS"."|"."$information";
fwrite("$openfile", "$what_to_write");
fclose($openfile);
&#125;


?> 

<? 
&#125;else&#123; 
?> 
You have either entered the incorrect login/password, or have not filled in one of the form fields. 
<? 
&#125; 
?>
this will write the information to the file; make sure to edit the name of the text or html file to your liking.

Code: Select all

<?
//EDIT THESE
$filename = "nameoffile.txt";
//STOP EDITING

$f_content = file($filename);
$f_content = array_reverse($f_content);
$a_num = count($f_content);

echo "<table><tr><td>Team<td>HCS<td>OTS<td>Information<tr>";

for ($t = 0; $t < $a_num; $t++) {	
$f_array = explode("|", $f_content&#1111;$t]);	
echo "<td>".$f_array&#1111;1]."<td>".$f_array&#1111;2]."<td>".$f_array&#1111;3]."<td>";
echo "$f_array&#1111;4]"."<tr>";
}
echo "</table>";

?>
this should read the file, and print out the newest entrys first, in a neat organized table.


if i have mistakes, would some php master please correct me, i realize i probably have a couple
Jimeee_1999
Forum Newbie
Posts: 4
Joined: Tue May 28, 2002 8:33 am

Thank you

Post by Jimeee_1999 »

Great! Thanks!
I really appreciate all of your help.

Jim
Post Reply