Flatfile Help
Moderator: General Moderators
Flatfile Help
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.
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.
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;
}
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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];
?>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);
}
?>http://custompc.discardedsoftware.com/?i=custom
Does the below make any difference?
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?
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);
}
?>