[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2003-12-03
8
Medium Priority
?
386 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
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 460 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 16 hours left to enroll

834 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