Solved

PHP - Flat files, calling data from a particular line by line number

Posted on 2003-12-03
8
374 Views
Last Modified: 2006-11-17
I have several php pages called with a variable attached called ID...

i.e. contact.php?ID=4

I need to would then need the php code to get the record from the 4th line (because ID=4) of a file called cities.txt. This record has 3 parts to it seperated by ":" I want to call "title", "city" & "state".

This is what I have...

<?php

$filename = "/html/cities.txt";
$fp = fopen($filename,"r");
$contents = fread($fp, filesize($filename));
fclose($fp);

$record = explode(":", $contents[$ID]);
 
$title = $record[0];
$city = $record[1];
$state = $record[2];

?>

This is returning nothing, plus I don't really want to read the entire file into $contents when I only want a particular line from the file. Please help.

Thanks in advance,


Dean
http://www.dcwebsitedesign.com
0
Comment
Question by:challenged
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 13

Expert Comment

by:lozloz
ID: 9872027
why have you got $contents[$id]? it should be $contents for the explode function. plus you're not printing anything so there won't be any output, unless this is a different version of the script

loz
0
 

Author Comment

by:challenged
ID: 9872178
You'll have to spell it out for me, cuz i'm kinda new...

"you're not printing anything so there won't be any output"
 - by no output, I mean that $title, $state and $city are all come out empty with the code I have now.

"why have you got $contents[$id]?"
 - Because I don't know what i'm doing... that's why i'm here :)

Can u please show me what the code should be?

Thanks, Dean
0
 
LVL 13

Accepted Solution

by:
lozloz earned 115 total points
ID: 9872229
sure, i'll explain it for you. best to use file for your purpose instead of fopen/fread, then you can use $contents[0] for line 1

<?php

$filename = "/html/cities.txt";
$contents = file($filename); // read it into the variable contents (the whole thing)
$record = explode(":", $contents[0]); // split the first line (index 0 of the array) by character :
 
$title = $record[0]; // setup the vars
$city = $record[1];
$state = $record[2];

// print them out:

print "title: " . $title . " - city: " . $city . " - state: " . $state;

?>

cheers,

loz
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:challenged
ID: 9872444

Thanks... it works, but only when I call the filename as...

$filename = http://www.home-search-experts.com/cities.txt";

When I look at my FTP program it says cities.txt is in the /html directory do you know what I should have put or should I contact my host?

I don't want to call the URL because I think it will make the page load slightly slower as the server has to run another process right?

Thanks for your help - other than that it works great!

Regards,

Dean
0
 
LVL 2

Expert Comment

by:nsanden
ID: 9873023
Try this:

$filename = $_SERVER['DOCUMENT_ROOT'] . "/cities.txt";
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9873886
if you're running your script from the root below the html directory, presumably you won't be able to access it from the web on that domain? nsanden's suggestion should work, although it's better to use \\html\\cities.txt in that case if you're on windows

loz
0
 
LVL 6

Expert Comment

by:aolXFT
ID: 9877509
if you don't want to get the entire file into memory, I suggest you use the pack/unpack functions and make every line the same length, and use fseek to get to that line, and fgets to load that line into memory.
0
 

Author Comment

by:challenged
ID: 9886307
I just asked support for the root... thanx guys... I thought i closed this out...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question