Solved

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

Posted on 2003-12-03
8
377 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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