There are 2 commonly used data management system. a) Using a Database System ( Oracle 9i, mysql, MsAssess postsql etc ) b) Using a flat file (text file)
In this tutorial I will start with data management using flat file
a) First you must plan what are the data you need to store in into a text file eg : If i want to create a information for my blog script with the following information a) Poster b) date c) title d) content e) image
( I will use this structure in my explanation for the rest of the tutorial )
b) You need to identify a seperator for the info (fields) mentioned in item a in this case will use `|` ;
My text file will have the following content. save this file under name : blogfile.txt
blogfile.txt - Make sure CHMOD is 766 or 777
Code:
Sean|16 May 2003|Testing|How are you today|http://www.google.com/nav_first.gif Sean|16 May 2003|Testing 2|I'm fine|http://www.google.com/nav_first.gif
Now that you have th data ready... it' time to write a php script to read & print
In your script
You need to tell your script to a) open the file
Code:
$opFile = "blogfile.txt"; $fp = fopen($opFile,"r") or die("Error Reading File");
where $opFile is your blogfile.txt mentioned above
Then you ask your script to dump content of the file to variable $data & close it after reading
Sean|16 May 2003|Testing|How are you today|http://www.google.com/nav_first.gif Sean|16 May 2003|Testing 2|I'm fine|http://www.google.com/nav_first.gif
b) you need to split up the $data into lines
Code:
$line = explode("\n", $data);
\n is actually a representation of "enter" or break line explode will split up the $data which that are \n and put it into an array ($line)
content of line as follows
Code:
$line[0]=Sean|16 May 2003|Testing|How are you today|http://www.google.com/nav_first.gif $line[1]=Sean|16 May 2003|Testing 2|I'm fine|http://www.google.com/nav_first.gif
c) Now you need to split it up again into i) Poster ii) date iii) title iv) content v) image
Let study the script below 1) Check the number of lines
Code:
$i=count($line); <--- In our examples there are only 2 line so $i = 2
2) Loop to split (explode) line to individual data or fields where there is '|'
Code:
for ($n=0 ; $n < $i-1 ; $n++ ) { $blog = explode("|", $line[$n]); <----this split (explode) $line into fields using | as seperator
if (isset($blog[0])) <--- Check whether blog[0] is set ! {
That's it !!!! Your own PHP blog script ! You can use a form to put data into you blogfile.txt or do it offline & upload the txt file !... you can expand this script into wonderful things !!
Full script here. You are free to use & modify this script... adding CSS, Table.. whatever. I dont mind if no credit is given but if there is , it would be nice !
Code:
<html> <head> <title>Sean's simple Blog Script tutorial using flatfile</title> </head> <body> <? echo "<font face=verdana>"; echo "<u><b>Tutorial - Sean's simple Blog Script Tutorial using flatfile</b></u><br><br>"; // Determines File you want to use $mode = 0; if ($mode == 0) { $opFile = "blogfile.txt"; } if ($mode == 1) { $opFile = "blogfile1.txt"; }
// Opens Blog File to read or dies $fp = fopen($opFile,"r") or die("Error Reading File"); $data = fread($fp, filesize($opFile)); fclose($fp);
// Explodes data at line breaks $line = explode("\n", $data); $i=count($line);