Page 1 of 1

Flat File Database - Edit a Line... ???

Posted: Tue Oct 31, 2006 7:28 pm
by buildakicker
Hello All,

I have been working on this forever now and I cannot seem to get this to work.

I have a flat-file db that I can work with, nothing else, I know, it's terrible.

I have created a form for a user to input data into, it saves as a text file with a delimiter. There are about 25 fields on the form.

I now want to be able to edit those fields. This seems straigt forward enough, but I cannot get it.

Here is my code, I have broke it down to just one item to fill in the text file for simplicities sake:

Code: Select all

//open file and populate array with data
$fh = fopen("timberdata.txt", "rw") or exit("Unable to open file!");

//this gives me the line# and fills $var with the first item of each line in the text file 
//the item I would like to find in order to edit the file, it displays it for the user to type the correct 
//information into the form below in order to find the correct item to edit
$i=0;
while(!feof($fh)) {
	$line_of_text = fgets($fh);
	$parts = explode('|', $line_of_text);
	$var[$i] = $parts[0];
	echo $var[$i] . '<br/>';
	$i++;
}
//close file
fclose($fh);
?>

//FORM TO ACCEPT NAME FOR EDITING, This name is then run against my if() statement below to see if
//the name exists
<br />
Type a Sale Name, exactally as seen above, in here: <br/>
<form action="<?php $_SERVER["PHP_SELF"]; ?>" method="post">
<input name="selectSaleName" type="text" size="20" />
<input name="confirm" type="submit" value="confirm" />
</form>
<br/>

<?php
//check out the posting
if (!empty($_POST['confirm'])) {
	$selectSaleName = trim($_POST['selectSaleName']);
	//put file information in array
	$lines = file("timberdata.txt");
	
//run through the lines in the file and give line a number if you want to see it... 
//I am doing this so I can possibly edit the line#?????
	foreach ($lines as $line_num => $line) {

		//explode file into $parts array
		$parts = explode('|',$line);		

		//found the matching Sale Name!
//NOW I WANT TO PUT ALL THE DATA IN THE $parts[] arrray into the Form Fields Below
		if($selectSaleName == $parts[0]){
			echo '<p class="warning">'. "THIS ONE! " . $parts[0] . '</p>';
			?>
//MY FORM TO GET DATA FROM
			<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" name="timbersales" id="timbersales" class="cssform">
			<input name="salename" tabindex="1" type="text" size="40" value="<?php echo $parts[0]; ?>" />
            <input type="submit" name="submit" value="Submit" />
 			</form>

//POST TO THE FILE -WHY WONT THIS WORK?????
			<?php
			if (!empty($_POST['submit'])) {
				$fp = fopen('timbersales.txt', 'w');
				fwrite($fp,$salename);
				echo '<p class="warningsm">' . $selectSaleName . ' Data Updated as of: ' . $date . ' <a href="logout.php">LOGOUT HERE!</a>' . '</p>';
				fclose($fp);
			}
		}
	}
	//this Sale Name doesn't exist! Break!
	if($selectSaleName != $parts[0]){
		echo '<p class="warning">' . $selectSaleName . ' does not exist in the database.'.'</p>';
		break;
	}
}
You can put a file called timbersales.txt in a folder and this code too and see it sort of work, but then break...

Any help would be great... even going a difffernet way??

Thanks!

Posted: Tue Oct 31, 2006 9:59 pm
by printf
So you know, I do understand what your doing, but if you really want the fastest possible flat file db that is equally as fast when reading and writing, then you should preset column widths, which will allow you to fread using fseek and rewind, which allows for real inline editing, which is something you can not do with a unformatted file.

printf

tell me more!

Posted: Tue Oct 31, 2006 10:47 pm
by buildakicker
I haven't heard any of that before, I will implement it.

How do I start?

Thanks so much for commenting!

Posted: Wed Nov 01, 2006 2:03 pm
by feyd
things to look at: fopen(), fread(), fseek(), fwrite(), fclose(), sprintf(), and fprintf(). There are likely others, but those are a start.