Pages

Friday, October 29, 2010

Flat File Databases

A Flat File database is a simple text file used to store information, and is most often used when you do not have access to a MySQL database, as they require no server addons other than PHP. They are best for smaller amounts of data than a MySQL database is capable of, and have fewer handling functions, but are very handy for things like counters and tagboards.
Writing Data
Say you have a form that collects a user's name and email address, and you want to save them to a flat file database. This is the code you'd point the form to:
<?php

$name = $_POST['name'];
$email = $_POST['email'];

$fp = fopen("data.txt","a");

if(!$fp) {
    echo 'Error: Cannot open file.';
    exit;
}

fwrite($fp, $name."||".$email."\n");

fclose($fp);
?>
The $name = $_POST['name']; parts collect the information submitted in the form on the previous page, just rename "name" to the name of the variable.
So what's fopen()? This part of the code opens the database file, in this case "data.txt." The "a" after the filename is a mode which indicates that we are opening this file for for writing only, and that if the file doesn't exist it should be created on the spot. A detailed list of fopen() modes can be found at php.net.
Once the database file has been opened, we can write the data to it using the fwrite() function. Here the script will add $name."||".$email."\n" to "data.txt", which will be written as "name||email" on its own line.
Once several people have filled out the form, you will have a flat file database containing some information like this:
Kali||kali@xentrik.net
User||user@hotmail.com
User2||user2@yahoo.com
Now that we have our flat file database filled with raw data, it's time to read and display it. The easiest way to do so would be this:
<?php
    readfile("data.txt");
?>
However that would simply display the data straight from the file, all on one line, which would be ugly and rather useless. What we need to do is format our raw data for output. In order to do that, we need to separate our lines of data using file().
<?php
$userinfo = file("data.txt");
?>
This will take the data out of the flat file into an array, resulting in something like this:
$userinfo[0] = "Kali||kali@xentrik.net";
$userinfo[1] = "User||user@hotmail.com";
$userinfo[2] = "User2||user2@yahoo.com";
Now that we have separated the rows of data, we need to separate the "columns" of data into individual variables, one each for $name and $email. To do this we will use the explode() function, which will use the || symbol between the pieces of data to break them up into variables. To do this we will need to create a loop, which will continue breaking up our rows until there are no rows left.
<?php
$userinfo = file("data.txt");

foreach($userinfo as $key => $val) 
{
   $data[$key] = explode("||", $val);
}
?>
Now you have an array like this:
$data[0][0] = "Kali";
$data[0][1] = "kali@xentrik.net";

$data[1][0] = "User";
$data[1][1] = "user@hotmail.com";
And so on, for every row of data.
Now, to output that data, we will create another loop to continue writing out the rows until there are no rows left. So the final code, including opening the file, reading the data from it, breaking it up and displaying it in a table, will look like this:
<?php
$userinfo = file("data.txt");

   echo '<table>';

foreach($userinfo as $key => $val) 
{
   //explode that data into a new array:  
   $data[$key] = explode("||", $val);
}

for($k = 0; $k < sizeof($userinfo); $k++) 
{ 
   echo '<tr><td>Name:</td><td>'.$data[$k][0].'</td></tr>';
   echo '<tr><td>Email:</td><td>'.$data[$k][1].'</td></tr>';
   echo '<tr><td colspan=2> </td></tr>';
}

   echo '</table>';
?>
So for every $k, or row of data in our flat file, the loop will write a new table row for name and email.
And that's it!

0 Comments:

Post a Comment