Flatfile Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Rob
Forum Commoner
Posts: 33
Joined: Fri Oct 03, 2003 3:18 pm

Flatfile Help

Post by Rob »

I need help with reading my flat file, I got the writing down, but I want to be able to read a single line, and parse it like Data[1], Data[2] and so on.. ive tryed a million different ways and I cant get it to work at all.. Im trying to have the line to be read put in thru the URL, like http://www.test.com/view.php?id=3 would show me line 3 in raw form (name||phone||blah||)

I can parse the array myself, I just cant get it to read only the line passed by $id, I hope I was clear enough, Thanks in advance.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

A few ways, here's one:

Code: Select all

<?php
if(!empty($_GET['id']) && ctype_digit($_GET['id'])){
  $theline = join('', array_slice(file('flatfile.txt'), $_GET['id']-1, 1));
  echo $theline;
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

<?php
$filename = 'somefile.dat';
$lines = file($filename);
$id = ( !empty($_GET['id']) ? (int)$_GET['id'] : -1);

if($id < 0)
  echo implode("<br />\n",$lines);
else
  echo $lines[$id];

?>
User avatar
Rob
Forum Commoner
Posts: 33
Joined: Fri Oct 03, 2003 3:18 pm

Post by Rob »

Thanks guys, one more question, when I write my flatfile I have some text boxes on the form with multi line, how can I keep it single line in the file?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You could run the text through nl2br() before writing, that will put <br />'s in the flat file instead of newlines, just means you'll have to str_replace() the <br />'s with newlines when you read it back in or you'll loose the line breaks.
User avatar
Rob
Forum Commoner
Posts: 33
Joined: Fri Oct 03, 2003 3:18 pm

Post by Rob »

I have tryed that allready, it left huge gaps in anyway (and did the </br> everywhere)
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Well it should only put a <br /> in place of the newlines if you put the nl2br() call in the right place ... what's the snippet of code where you do the nl2br then write the file out?
User avatar
Rob
Forum Commoner
Posts: 33
Joined: Fri Oct 03, 2003 3:18 pm

Post by Rob »

Code: Select all

<?
if ($submit) {
$entry_line = "$name||$phone||$email||$case||$drives||$hds||$memory||$mobo||$processor||$video||$monitor||$sound\n";
$fp = fopen("data.pl", "a");
    fputs($fp, $entry_line);
    fclose($fp);
}

?>
THeres the code I use to write..heres the form
http://custompc.discardedsoftware.com/?i=custom
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Does the below make any difference?

Code: Select all

<?
if ($submit) {
$entry_line = "$name||$phone||$email||$case||$drives||$hds||$memory||$mobo||$processor||$video||$monitor||$sound";
$fp = fopen("data.pl", "a");
    fputs($fp, nl2br($entry_line)."\n");
    fclose($fp);
}

?>
Also, i meant to ask .. why do you want them all on the same line? Is this so you can read them all back in using file() or .... something?
Post Reply