Page 1 of 1
Flatfile Help
Posted: Tue Jul 06, 2004 2:18 am
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.
Posted: Tue Jul 06, 2004 2:29 am
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;
}
?>
Posted: Tue Jul 06, 2004 2:33 am
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];
?>
Posted: Tue Jul 06, 2004 11:21 am
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?
Posted: Tue Jul 06, 2004 11:26 am
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.
Posted: Tue Jul 06, 2004 1:33 pm
by Rob
I have tryed that allready, it left huge gaps in anyway (and did the </br> everywhere)
Posted: Tue Jul 06, 2004 1:38 pm
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?
Posted: Tue Jul 06, 2004 6:11 pm
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
Posted: Tue Jul 06, 2004 6:13 pm
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?