DATE & TIME FUNCTIONS

 

Flat Files are simple text files or any other file used to store information.

Flat Files are simple text files or any other file used to store information. They are often a good alternative to a MySQL DB because they are supported without any other addons to PHP. They are also very good for storing small amounts of information, where a DB can store a ton of information without a problem. A file is not searchable like a DB, hence another drawback. Flat Files are just nice simple things that are better for smaller scale projects or scripts. In this tutorial I will show you how to deal with more complex file functions and data outputting.

I will assume you are familiar with the basics of open/reading/writing to a file, if not here is a very quick review:
fopen() - opens a file.
fwrite() - writes to a file.
fread() - reads a file.
file() - puts each line of a file into an array.

Quick Huh? Lets say you have a file called data.txt. In it you have some information like this:

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

Each of those is on it's own seperate line. That's all well and good but, what how can I use that? It just looks like raw data. And that sir is what it is! This data needs you to format it for output. Look back up to file(), it reads each line for a file into an array. So if we do that we will have something like this:
<?
$Lines = file("data.txt");
//will give a result like with these values
$Lines[0] = "Adman||16||PHP";
$Lines[1] = "Spoono||25||Graphics";
$Lines[2] = "Robouk||23||Photoshop";
?>
You have the data out of the file now. Think of each value of the $Lines array as a row in a database. Each row has three colums. Their Name, Age, and Hobby. You need to get the information out of that DB. This is where the explode() functions comes in. Explode() takes a variable or string, and looks for a string separator. In this case it is the double pipes (||). It breaks all the information around that into a seperate array. Here is what would happen if I took $Lines[0] and explode() it.
<?
$Data = explode("||", $Lines[0]);
//This is what the $Data array would look like
$Data[0] = "Adman";
$Data[1] = "16";
$Data[2] = "PHP";
?>
Great! Now you have to get each line exploded. You can't do it one by one, because you can never be exactly sure how many lines your file has. Think, its time for loops! You can use a foreach loop or a for loop. You need to loop through each line, and explode that into its own array.
<?
$Lines = file("data.txt");
//Foreach value in the array
foreach($Lines as $Key => $Val) {
//explode that data into a new array:  
$Data[$Key][] = explode("||", $Val);
}?>

Now you have an array like this:
<?
$Data[0][0] = "Adman";
$Data[0][1] = "16";
$Data[0][2] = "PHP";
?>
and so on and so forth. Now you need to output that data, a simple for loop will do the trick.
<?
for($K = 0; $K < sizeof($Lines); $K++) {
echo '<p>';   
echo 'Name: '.$Data[$K][0].'<br>'
echo 'Age: '.$Data[$K][1].'<br>'
echo 'Hobby: '.$Data[$K][2].'<br>'
echo '</p>';
}
?>

With a little thought, you can understand the example. That should get you started on making your own flat file scripts.
Hosted by www.Geocities.ws

1