Current location: Hot Scripts Forums » Programming Languages » PHP » working with a text file...


working with a text file...

Reply
  #1 (permalink)  
Old 05-18-04, 07:27 PM
jasong jasong is offline
Wannabe Coder
 
Join Date: Feb 2004
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
working with a text file...

How can i do it so i can open up a text file, and store the 1st line as $line1, the second line as $line2, etc for a script im doing?

For instance, i want if i had a text file that looke like
=======
pippin
frodo
samwise
merry
======

i could store line 1 as $name1, and then print $line1 etc.

Thanks,
Jason
Reply With Quote
  #2 (permalink)  
Old 05-18-04, 07:38 PM
Lost Lost is offline
Wannabe Coder
 
Join Date: Mar 2004
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
You will have to store them in an array.


<?php

//Open the file for reading
$getfile = fopen("somefile.txt","r");
$i = 0;
//Loop through each line of the file until the end
while(!feof($getfile))
{
//Set the value of that line to the array $file
$file[$i] = fgets($getfile);
//Echo the value of that line
echo $file[$i];
$i++;
}

?>
Reply With Quote
  #3 (permalink)  
Old 05-18-04, 08:18 PM
jasong jasong is offline
Wannabe Coder
 
Join Date: Feb 2004
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the reply!
is there a way i could make each line its own variable though?
like $name1, $name2, $name3 etc?
I want to be able to call these later.

Thanks!
Reply With Quote
  #4 (permalink)  
Old 05-18-04, 08:38 PM
Marshall Marshall is offline
Newbie Coder
 
Join Date: May 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
You can, using the code from above just do something like this
PHP Code:

$name1 $file[0];   // First line in text document

$name2 $file[1];   // Second line in text document
$name3 $file[2];   // Third line in text document 
etc.

There is probably a while loop or a foreach loop you can do this with as well but I dont know it off the top of my head.

The $file variable is made into an array in the code previously posted so it can store the entire text file with the lines seperated by adding the [#] to end of variable to select which line number u want (starting at 0 for line 1).
Reply With Quote
  #5 (permalink)  
Old 05-19-04, 01:10 PM
tylerc tylerc is offline
Newbie Coder
 
Join Date: May 2004
Location: Canada
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
try a mysql database, more efficient
__________________
-Tyler
RuneGuide Owner/Manager

Runescape Stats:
http://www.rsbandb.com/sig/sigs/n/nexon89.png
Reply With Quote
Reply

Bookmarks


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP code to edit a text file mdhall Script Requests 12 12-23-10 04:03 AM
MySQL Query problem Wraith PHP 5 03-06-04 05:16 PM
picking random entries with a filter... Double selection problem dsumpter PHP 7 11-16-03 07:19 PM
Editing a text file. cjh_60 PHP 1 10-14-03 03:59 PM
Writes to a text file gamextremer2003 JavaScript 4 09-11-03 09:43 AM


All times are GMT -5. The time now is 10:35 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.