Page 1 of 1

How do u write to a text file?

Posted: Tue May 28, 2002 8:33 am
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.
<?
}
?>

Posted: Tue May 28, 2002 9:14 am
by enygma
fopen/fwrite/fclose are your best friends.

check them out in the manual.

Posted: Tue May 28, 2002 9:30 am
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;

Posted: Tue May 28, 2002 1:54 pm
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

Thank you

Posted: Wed May 29, 2002 8:06 am
by Jimeee_1999
Great! Thanks!
I really appreciate all of your help.

Jim