How to do database with text file in PHP _php tips

Source: Internet
Author: User
Tags eval first row
According to my experience, I think that the following file structure is the best:
----------------------------------------------------------------------
File name extension:. php
? Die (' ACCESS denied! ');? >
Email=ask4more@13.net & Nickname=redfox & realname= Ardin & url=http://netnote.oso.com.cn & ...
...
----------------------------------------------------------------------
Maybe we all can see it. php to do the extension, and the first line of the file is Die (' ACCESS denied! ');? This effectively prevents illegal access to data files. The format of the second line of the file is: variable name 1= value 1 & variable name 2= value 2 & ...
It is very simple to propose all the variables, that is, to use the function parse_str ();
For example:
?
$theline = "Email=ask4more@13.net&nickname=redfox&realname= Ardin &url=http://netnote.oso.com.cn";
PARSE_STR ($theline)//Separating out variable $email, $nickname, $realname, $url
echo "I am $nickname, my real name is $realname <br>";
echo "Welcome to visit my website: $url <br>";
echo "Email me at: $email";
?>
Run Result:
I am redfox,my real name is Ardin
Welcome to visit my website:http://netnote.oso.com.cn
Email me at:ask4more@13.net

Therefore, this article provides that the data text structure is:
----------------------------------------
? Die (' ACCESS denied! ');? >
Variable name 1= value 1 & variable name 2= value 2 & ...

File name extension:. php
----------------------------------------

The real data starts with the second line. Well, with such a file structure can be easily implemented Guestbook,bbs, even the community's data processing: my homepage, "Network sticky note" http://netnote.oso.com.cn, this is achieved.
In order to facilitate the vast number of netizens, I made a number of functions, the following will make the necessary explanations. Of course you can modify and take the shell casually, but you must ensure the integrity of the function. Please save the following code as TEXTFUN.INC (and, of course, other names are the same), add a line to the beginning of the file you want to use <?include ("Textfun.inc"); You can use the function I have made for you.
Here is a total DB object, a function p2row ();

-------------textfun.inc----------------
?
Class db{
var $dbfile;
function Createdb ($dbName) {
$f = $dbName;
$this-> $dbfile = $f;
$headInfo = "<?die (' ACCESS denied! ');? >\n ";
$FP =fopen ($f, "w");
Fputs ($fp, $headInfo);
Fclose ($FP);
chmod ($f, 0777);//modify file mode, also available under Unix
return (1);
}
function Opendb ($f) {
$this-> $dbfile = $f;
if (file_exists ($f)) {
return true;
}else{
$this->createdb ($f);
}
}
function Insertline ($info) {
$fields =explode ("|", $info);
while (list ($key, $val) =each ($fields)) {
$therow. = "$val =\$". $val. " & ";
$var 1.= "\$". $val. ",";
}
$var 1.= ' $tail ';
Eval ("Global $var 1;"); In order to obtain environment variables
Eval ("\ $therow =\" $therow \ ";");
$FP =fopen ($this-> $dbfile, "a");
Fputs ($fp, "$therow \ n");
Fclose ($FP);
}
function ReadAll ($f) {
if (file_exists ($f)) {
$this-> $dbfile = $f;
$rows =file ($f);
for ($i =1; $i <count ($rows); $i + +) {
$temp []= $rows [$i];
}
return $temp;
}
}
Read all data rows in reverse order
function Revread ($f) {
if (file_exists ($f)) {
$this-> $dbfile = $f;
$rows =file ($f);
$d =count ($rows);
$j = $d-1;
for ($i =0; $i < $d; $i + +) {
if ($i < $j) {
$temprow = $rows [$i];
$rows [$i]= $rows [$j];
$rows [$j]= $temprow;
$j--;
}
}
For ($i =0 $i <count ($rows)-1; $i + +) {//Remove first line
$temp []= $rows [$i];
}
return $temp;
}
}

function Close () {
$this = $nothing;
}
}

Format paragraph text into one line of text for easy storage
function P2row ($t) {
$t =nl2br (Stripslashes (Htmlspecialchars ($t)));
for ($i =0; $i <strlen ($t); $i + +) {
$c =substr ($t, $i, 1);
if (Ord ($c) ==10) $c = "";
$tempstr. = $c;
}
return $tempstr;
}
?>
----------------------------------

DB is our custom data object for this article, including six methods: Createdb (), Opendb (), Insertline (), ReadAll (), Revread (), close ();

Db->createdb (string filename)
Use Example:
Include ("Textfun.inc");
$mydb =new DB;
$mydb->createdb ("userinfo.php");
?>
This method creates a file userinfo.php, the first line is Die (' ACCESS denied! ');? >

DB-&GT;OPENDB (string filename)
Use Example:
Include ("Textfun.inc");
$mydb =new DB;
$mydb->opendb ("userinfo.php");
?>
This method "opens" The data file userinfo.php in Append mode and is created if the file does not exist.
Therefore, this method can replace the Createdb () method. (but do not delete the class db{} inside the Createdb () function OH: P)

Db->insertline (String varstring)
Use Example:
Include ("Textfun.inc");
$theline = "Email=ask4more@13.net&nickname=redfox&realname= Ardin &url=http://netnote.oso.com.cn";
Parse_str ($theline);//Construct environment variable
$mydb =new DB;
$mydb->opendb ("userinfo.php");
$mydb->insertline ("Nickname|realname|email|url");
?>
The Db->insertline () can separate the corresponding environment variable from the string of "Nickname|realname|email|url", and deposit it in the form of the agreement of this article. To pass in the Insertline () parameter, be sure to use the "|" The environment variable name into a string, the number of unlimited, but do not add "$" in front, ah, is to form such as "Nickname|realname|email|url" such a string: ~)

Array Db->readall (string filename)
Use Example:
Include ("Textfun.inc");
$mydb =new DB;
$allrec = $mydb->readall ("userinfo.php");
?>
ReadAll () method returns except the first row (Die (' ACCESS denied! ');? >) An array of all the data, each row corresponds to an element of the array.

Array Db->revread (string filename)
Use Example:
Include ("Textfun.inc");
$mydb =new DB;
$allrec = $mydb->revread ("userinfo.php");
?>
The Revread () method reads the first line in reverse order (die (' ACCESS denied! ');? >) All data outside, return array. This is especially useful for us when we are making a message.

void Db->close ()
Closes the DB object.

OK, now we're going to use the DB object to make up the simplest message book.
---------guestbook.php------------
My message book <p>
<form Name=form1 action=<? echo $PHP _self;? >>
Nickname:<input Type=text name=nickname><br>
E-mail:<input Type=text name=email><br>
Homepage:<input type=text name=url value= "http://" ><br>
Message:<textarea name=message cols=30 rows=12></textarea><p>
<input type=submit Name=submit value= Submit >
</form>
?
Include ("Textfun.inc");
if ($Submit) {
$thetime =date ("y-m-d h:m:s A");
$message =p2row ($message);
$mydb =new DB;
$mydb->opendb ("msg.php");
$mydb->insertline ("Nickname|email|url|message|thetime");

read out all the data below
$allrecs = $mydb->revread ("msg.php");
while (list ($key, $theline) =each ($allrecs)) {
Parse_str ($theline);
?>
<a href= "Mailto:<?echo $email;? > "><?echo $nickname;? ></a><?echo $thetime;? ><br>
Url:<a href= "<?echo $url;? > "><?echo $url;? ></a><br>
Message:<br><?echo stripslashes ($message);? &GT;&LT;HR NoShade size=1>
?
}
$mydb->close ();
}
?>
-----------------------------
Well, although this message is not very beautiful, but mainly to illustrate the use of DB objects ~:)
This article under the WIN98+PWS+PHP4 debugging Pass!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.