Solved

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

Posted on 2003-12-03
8
370 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:challenged
Comment Utility

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 2

Expert Comment

by:nsanden
Comment Utility
Try this:

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

Expert Comment

by:lozloz
Comment Utility
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
Comment Utility
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
Comment Utility
I just asked support for the root... thanx guys... I thought i closed this out...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 count occurrences of each item in an array.

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now