Updating a flat file database

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

I'm doing the final project for my Web Design course and I have no idea how to update a flat file database.
Can any one point in the right direction?

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Does XML count as a flat-file database?

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Maybe, but I am using a .txt file (you know that) Sticking out tongue

I have only a week or so left for this project, any one have any idea how to do it?

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Maybe? I didn't know it was a .txt file -- does it have to be one?

What environment is this? Do you have PHP XML libs? expat, sablotron?

KarenArt's picture

She has: 354 posts

Joined: May 2001

How are you creating your flat files?
Are you using php, asp or pearl?

If you show me the code you use to create and read from the db, I can show you how to update it.

Abhishek Reddy, I've never used XML so I'm not sure but, there are quite a lot of tutorials on how to convert flat files to XML so they don't seem to be the same thing. Sounds like XML is a step up.

gotta finish redesigning my sites so I can show them again.

The purpose of education is... to get more jokes!

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

This is how I am reading the db:

<?php
 
// function used to output the vacancies page from flat file database
 
function fnShowVacancies($file, $show, $show_queries)
    {
     
// opening file...
     
$filetr = file($file, \"r\");
      // looping through array
      foreach(
$filetr as $id => $string) {
         
$details = explode(\" +~+ \", $string);
          if(
$id >= $show && $id < ($show + $show_queries) || $show == \"all\") {
              echo \"<tr>\";
              echo '<td><img src=\"./images/' .
$details[0] . '\" width=\"' . $details[1] . '\" height=\"' . $details[2] . '\" alt=\"' . $details[3] . '\" title=\"' . $details[3] . '\" /></td>';
              echo '<td><h6>Reference</h6>';
              echo
$details[3];
              echo '<h6>Address</h6>';
              echo
$details[4] . '<br />';
              echo
$details[5] . '<br />';
              echo
$details[6] . '<br />';
              echo
$details[7];
              echo '<h6>Rent</h6>';
              echo
$details[8];
              echo '<h6>Comments</h6>';
              echo
$details[9];
              echo \"</tr>\n\";
            }
        }
      // closing file
      @
$filetr = fclose($filetr);
    }
?>

Abhishek, KarenArt: I am using PHP to do this Sticking out tongue Laughing out loud

KarenArt's picture

She has: 354 posts

Joined: May 2001

I believe what you're looking for is fwrite.

I was going to post some code for you, but these pain pills have me so messed up I doubt if I'd get it right.

There is a really good explination of fwrite at http://us3.php.net/fwrite

Here's a full tutorial on working with flat files http://www.spoono.com/php/tutorials/tutorial.php?id=32 This should explain what each part of the code does.

If that doesn't work for you, let me know and I'll try to post the code for you.

Have fun with it! Laughing out loud

gotta finish redesigning my sites so I can show them again.

The purpose of education is... to get more jokes!

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Thanks for the links KarenArt, I had a look at them but it still doesn't tell me up to update the flat-file :S

Perhaps I am not making myself clear...

Say I have the following in my db.txt file:

Adman||16||PHP
Spoono||25||Graphics
Robouk||23||Photoshop
'

And I would like to change it to this:

Adman||16||Graphics
Spoono||25||PHP
Robouk||23||Photoshop
'

How would I do that?

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

First, init the "database": read the file line-by-line into an array; split each array item by "||" so it becomes a multidimensional array, as though it were a result returned by a MySQL 'SELECT *'.

From there, use PHP to alter array element values as needed.

Last, reconstruct the whole file from the array you have.

The reading of the whole file, then writing it all again is what causes extra server overhead with flat-file databases. This, I believe, can be circumvented if you use self-aware, flat documents (i.e. XML), so data fields can be located and modified easily -- even using another layer of abstraction (XML libs). Although, Karen might be right about the distinction between XML and flat-file. Again, are you restricted from using XML?

Alternatively, you could look into PHP dba. I've never used it, but it looks like it handles flat-file databases too.

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Yes, I think I am restricted from using XML. I can easily have it on my computer, don't know about the school or host though. Besides, if I get into XML, it'll take me a while to figure everything out and get used to it - no matter how easy it seems. I don't have that much time left or I probably will try XML. :S

I got that far but I don't know the actual code to do it, I am still very new to arrays and databases. What functions do I acutally use to achieve it? Can any one produce any code? Someone put me out of my misery! lol Sticking out tongue

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Read the comments on the filesystem functions pages... Use explode() to split the strings. Meddle with the values as you would any basic array variable. Write to the file as explained in the fwrite() PHP doc... it is really basic.

Use file_get_contents() to read the contents of the file into a string.

explode("\n",$db_string) it to create an array containing lines (records) of the database.

explode("||",$db_string[$i]) in a foreach{} or other loop to recursively split each record into fields.

Now you can access each element via $db_string[y][x] -- this is how you alter the values.

Once that's done, loop through the whole array, reconstructing the database string, perhaps using implode(). fwrite() or file_put_contents() that back into the file, not appending but overwriting.

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Eh! I must be really stupid or I really do not understand, any one have any code they can show me as an example?

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

<?php
$my_db
= file_get_contents($path);

$my_db = explode(\"\n\", $my_db);

foreach (
$my_db as $id => $record) {
 
$my_db[$id] = explode(\"||\", $record);
}

$my_db[0][1] = \"my new value\";
$my_db[5][2] = \"my new value\";

foreach (
$my_db as $id => $record) {
 
$my_db[$id] = implode(\"||\", $record);
}

$my_db = implode(\"\n\", $my_db);

file_put_contents(
$path, $my_db);
?>

No guarantees, it is untested, may need tweaking. Try it.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.