| Home | Hotscript | Downloads  
Text Link Ads

Tutorial - PHP
Categories
Blog [2]
Communication [1]
Site Stats [3]
Skin/Template [2]
Timer & Counter [1]
Tools [3]
Tutorial - Photoshop
Categories
Graphics [5]
Special Effects [2]
Text Effect [2]
Download - PHP Scripts
myBloggie - D/L
Advanced Blog System
myBloggie's Home
myBloggie's Demo
user: guest
pwd: pass
myEvent
Dynamic Calendar with Events
Bloggielite
New Generation Blog
TNX?
Foam Mattress
Anguilla Hotels
Patio Heaters
Yoga Mats

PHP/MySQL Tutorial
 
Simple FlatFile Based Blog Script
Written by : Sean
Date : 2003-07-07
[Back]  [Home]  


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 Smile

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
Code:
$data = fread($fp, filesize($opFile));
fclose($fp);


Now $data will have the following content
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


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 !
   {
   
    //Display results //
    echo "Posted by : " .$blog[0]."<br>";
    echo "Date : " .$blog[1]."<br>";
    echo "Title : " .$blog[2]."<br>";
    echo "Message : " .$blog[3]."<br>";
    echo "Avatar : <img src='$blog[4]'><br><br>";

    }

 }




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 Smile !

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);

for ($n=0 ; $n < $i-1 ; $n++ ) {
  $blog = explode("|", $line[$n]);

  if (isset($blog[0]))
   {
    echo "Posted by : " .$blog[0]."<br>";
    echo "Date : " .$blog[1]."<br>";
    echo "Title : " .$blog[2]."<br>";
    echo "Message : " .$blog[3]."<br>";
    echo "Avatar : <img src='$blog[4]'><br><br>";

    }
 }
   echo "<center>© Copyright 2003 Sean's script</center></font>"
   ?>
</body>
</html>


That's all folks .... happy scripting
Sean



myWebland © 2003, 2004 myWebland Group
myWebland | wahsei | Contact Us | Privacy Statement