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
Solved

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

Posted on 2003-12-03
8
373 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 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.

792 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