Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Pulling data from a text file
Old 11-18-2007, 02:01 PM Pulling data from a text file
Yak Yak Yak Yak Yak

Posts: 593
Location: Rochester, MN
Trades: 0
If I have a text file that has a dozen lines/rows, how do I select a specific row as an array?

For example the following | delimited file:

1234|abc1234|def1234
1235|abc1235|def1235
1236|abc1236|def1236
1237|abc1237|def1237
1238|abc1238|def1238

How would I select the line that starts with 1237, and echo column 3? I know the echo would look something like this: echo column [2], but I don't know how to select the row I want parsed into an array.

Can someone point me in the right direction?
__________________

Please login or register to view this content. Registration is FREE
neorunner is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-18-2007, 02:13 PM Re: Pulling data from a text file
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
You need 2 things:
1) iterate through lines
2) convert the line to an array

For the point 1, use fgets()
http://www.php.net/manual/en/function.fgets.php
For the point 2, use explode()
http://www.php.net/manual/en/function.explode.php

PHP Code:
$h=fopen("file.txt",r);
$run=true;
while( (
false!=$buffer=fgets($h)) && ($run==true) ){
  
$ary=explode("|",$buffer);
  
//... Test your condition here against $ary
  
if($ary[0]=='1237'){
    echo 
"found it! ->{$ary[2]}\n";
    
//To stop the file parsing
    
$run=false;
  }
  else{
    echo 
"not found...\n";
  }
}
fclose($h); 
Just a side note, array indexes starts from 0, so the first field is 0, and your data field is nbr 2.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 11-18-2007 at 02:17 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-18-2007, 02:21 PM Re: Pulling data from a text file
shivaji's Avatar
Ultra Talker

Posts: 321
Trades: 0
Hi,
I am prefer to use file function.

PHP Code:
<?php
// looking for 1237 line

$file file("data.txt");
foreach(
$file as $val) {
list(
$id$first$second) = explode("|"$val);
   if(
$id == "1237") {
      echo 
"Id = " $id "<br />First data = " $first "<br />Second data = " $second;
      break;  
// stop the loop
   
}
}
?>
Shivaji
__________________

Please login or register to view this content. Registration is FREE
- uncommon free scripts

Please login or register to view this content. Registration is FREE
- Städte, Sport, Party, Gourment, Apartments, Hotels
shivaji is offline
Reply With Quote
View Public Profile Visit shivaji's homepage!
 
Old 11-18-2007, 03:38 PM Re: Pulling data from a text file
Yak Yak Yak Yak Yak

Posts: 593
Location: Rochester, MN
Trades: 0
Which option is better under heavy server load? fopen or file?
__________________

Please login or register to view this content. Registration is FREE
neorunner is offline
Reply With Quote
View Public Profile
 
Old 11-18-2007, 04:02 PM Re: Pulling data from a text file
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Depend your file size.
Fgets parse the file line by line.
file() fetch all the file in memory.

It's the only significant difference, I'd say.
But on a 1+Mo file, it can be noticeable.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-18-2007, 06:08 PM Re: Pulling data from a text file
Yak Yak Yak Yak Yak

Posts: 593
Location: Rochester, MN
Trades: 0
Now that I figured that out, I have another problem. In column [0], the first row, I have a number. How do I check for the largest number in the column, and set a variable to be the largest number +1?
__________________

Please login or register to view this content. Registration is FREE
neorunner is offline
Reply With Quote
View Public Profile
 
Old 11-18-2007, 07:37 PM Re: Pulling data from a text file
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
In that case, you don't exit the parsing of the file when you look for your line, and you keep the greatest value in a variable.
But why do you bother with a text file?
For what I see, a database would be way more suited.
PHP Code:
$h=fopen("file.txt",r);
$run=true;
$max=0;
while( (
false!=$buffer=fgets($h)) && ($run==true) ){
  
$ary=explode("|",$buffer);
  
//... Test your condition here against $ary
  
if($ary[0]=='1237'){
    echo 
"found it! ->{$ary[2]}\n";
    
//To stop the file parsing
    //$run=false;
  
}
  else{
    echo 
"not found...\n";
  }
  if(
$max<$ary[0]){
    
//$max will hold the biggest value from your file after that.
    
$max=$ary[0];
  }
}
fclose($h); 
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 11-18-2007 at 07:38 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-22-2007, 04:47 AM Re: Pulling data from a text file
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Are you afraid of databases or your hoster does not offer mysql on free plan? Considering your current needs you will have to migrate to database anyway.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 11-23-2007, 09:52 AM Re: Pulling data from a text file
Yak Yak Yak Yak Yak

Posts: 593
Location: Rochester, MN
Trades: 0
No, this is just an attempt on my part to learn some basics of using text files. My host does provide mysql.

I am just trying to learn something new.
__________________

Please login or register to view this content. Registration is FREE
neorunner is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Pulling data from a text file
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.28723 seconds with 12 queries