Home > Backend Development > PHP Tutorial > PHP methods to implement five basic operations on text database_PHP tutorial

PHP methods to implement five basic operations on text database_PHP tutorial

WBOY
Release: 2016-07-13 17:19:37
Original
815 people have browsed it

来源:http://www.pchoer.com/

PHP实现对文本数据库的数据显示、加入、修改、删除、查询五大基本操作的方法
我用一个留言本程序作为例子,阐述PHP实现对文本数据库的数据显示、加入、修改、删除、查询五大基本操作的方法。
此文本数据库共有字段10个:客户IP、发言时间、客户名、客户EMAIL、客户主页地址、留言表情图片名、客户QQ、客户形象图片、留言内容、管理员回复内容。

1、加入数据程序段。
$date=date("Y-m-d H:i:s");//取得系统时间
$ip = $HTTP_SERVER_VARS[REMOTE_ADDR]; //取得发言的IP地址
$text=encode($gb_text);//去掉留言内容后面的空格.
$fp=fopen("gb.dat","a");//以只写模式打开gb.dat文本文件,文件指针指向文件尾部.
$str=$ip."|".$date."|".$gb_name."|".$gb_email."|".$gb_home."|".$face."|".$gb_qq."|".$head."|".$text."|".$reply." ";//将所有留言的数据赋予变量$str,"|"的目的是用来今后作数据分割时的数据间隔符号。
fwrite($fp,$str);//将数据写入文件
fclose($fp);//关闭文件
showmessage("留言成功!","index.php","3");//留言成功,3秒后自动返回主界面。
其中的$gb_name 、$gb_email、$gb_home、$face、$gb_qq、$head、$gb_text、$reply是由发言表单传过来的数据。

2、数据显示程序段
if (file_exists("gb.dat")){//检测文件是否存在
$array=file("gb.dat");//将文件全部内容读入到数组$array
$arr=array_reverse($array);//将$array里的数据安行翻转排列(即最后一行当第一行,依此类推)读入数组$arr的每一个单元($arr[0]...)。
$num=count($array);//获取数组$array里的信息数(一行为一条信息)
if ($num>0){//如果信息数大于零(即文本数据库不为空)
$total=ceil($num/$pagesize);//计算总页数(取最大整数,即凡有小数点都进一取整,$pagesize为预设的每页显示的信息数)
if($page<1){//如果当前页面数码号小于1
$page=1;//则赋值为1
}
$number=($page-1)*$pagesize;//计算当前所显示第一个留言的数码号(数码号从零开始,主要是达到与数组单元号对应的目的)
for($i=0;$i<=$pagesize-1;$i++){//进入循环
$row=explode("|",$arr[$number]);//以"|"作为分割符,分割数组$arr中每第$number个单元的数据,并将这些数据赋予数组$rom
list($ip,$datetime,$name,$email,$home,$face,$qq,$head,$text,$reply)=$row;//将数组$row里的单元数据按顺序赋予括号里的变量
?>
>//显示客户形象图片


昵称【
//显示客户名
发表于://显示留言发表时间


>//显示客户留言表情图片
说://显示客户留言内容


//显示回复内容


访问的主页//客户主页的超连接
发信//客户E-MAIL的连接
的QQ号码是//显示客户的QQ号码
的IP地址为" //显示客户的IP地址
回复//留言回复的连接语句
删除//留言删除的语句(以客户留言时间$datetime作为删除标识)


if ($number == $num-1)//如果数组的单元号等于总留言数减一(因为单元号以零开始的,所以这意味着这是最后一条留言)
{
break;//跳出循环
}
$number = $number + 1; //数组单元号加1
}//循环结束符
}
if ($page <> 1)//如果当前页面数码号不等于1
{
$back = $page - 1;//当前页面数码号减1,并将此值赋予变量$back
echo "第一页";//显示第一页的连接
echo " 上一页" ;当前页面数码号等于$back,并显示其连接
}
if ($page <> $total)//如果当前页面数码号不等于总页数号(最后一页数码号)
{
$next = $page + 1;//当前页面数码号加1并赋予变量$next
echo " 下一页" ;//显示下一页连接
echo " 最后一页"; 显示最后一页连接
}
echo "页数:$page / $total";//显示当前页面数码号和显示最后一页数码号
echo "共有 $num 条留言";//显示留言数信息

}
else {
echo "

当前没有任何留言!
";//如果文件内容为空则显示的信息
}
else {
echo "
数据文件丢失,请联系管理员!或发布留言重新建立数据文件!
";//如果文件不存在显示的信息
}

3. Data modification program segment
$list=file("gb.dat");//Read the entire gb.dat file into the array $list, each element of the array is a message ($list[0 ] is the data of the first message, $list[1] is the data of the first message....
$n=count($list);//Calculate the total number of messages in the $list content, and Assign the variable $n
if ($n>0){ //If the number of messages is greater than 0
$fp=fopen("gb.dat","w");//Open the file in write-only mode gb.dat
$gb_reply=encode($gb_reply);
for ($i=0;$i<$n;$i++) {//Enter the loop
if(eregi($ttime,$ list[$i])){//Compare the string matching time $ttime sent to send a message with the content in the array unit $list
$f=explode("|",$list[$i]); //If a match is found, use "|" as the delimiter to cut the message information $list[$i] (the $ith message), and assign these data to the array $f
$f[9]= $gb_reply;//Replace $f[9] (the last piece of data in the message) with $gb_reply (reply content)
$list[$i]=$f[0]."|".$f[. 1]."|".$f[2]."|".$f[3]."|".$f[4]."|".$f[5]."|".$f[ 6]."|".$f[7]."|".$f[8]."|".$f[9]." "; //Replace the contents of the array unit $list[$i] with the array $f plus the separator "|" (where $f[9] is the modified new data).
break; // Jump out Loop
}
}//Loop end character
}
FOR($i=0;$i<=$n;$i++){//Enter loop
fwrite($fp ,$list[$i]);//Create each unit of the array $list into one line and write it to the file gb.dat
}//Loop end character
fclose($fp);//Close the file
showmessage("Reply successfully!","index.php");//Reply successfully and automatically return to the main interface

4. Data deletion program segment
$list=file("gb.dat");//Read the entire gb.dat file into the array $list, each element of the array is a message ($list[0 ] is the data of the first message, $list[1] is the data of the first message....
$n=count($list);//Calculate the total number of messages in the $list content, and Assign the variable $n
if ($n>0){//If the number of messages is greater than 0
$fp=fopen("gb.dat","w");//Open the file in write-only mode gb.dat
for ($i=0;$i<$n;$i++) {//Enter the loop
if(eregi($ttime,$list[$i])){//Will send Come and send a message. The time $ttime is matched with the strings in the array $list[$i]
$list[$i]="";//If the match is successful, $list[$i] will be cleared ( To achieve the purpose of deletion)
break;//Jump out of the loop
}
}//Loop end character
FOR($i=0;$i<=$n;$i++){// Enter the loop
fwrite($fp,$list[$i]);//Convert each unit of the array $list into a line and write it to the file gb.dat
} //Loop end character
fclose ($fp);//Close the file
showmessage("Delete successfully!","index.php");//Delete successfully and automatically return to the main interface.
}


5. Data query program segment







/////////////////////// //////////The above is the search form statement segment
$id=0;
$list=file("gb.dat");//Read the entire gb.dat file to the array $list, each element of the array is a message ($list[0] is the data of the first message, $list[1] is the data of the first message....
$n=count($list);//Calculate the total number of messages in the $list content and assign it to the variable $n
$found=trim($found);
if (!$found){ //If $found is false
echo "
You did not enter any keywords!
";//For related display
}
else {
if($n>0 ){//If the number of messages is greater than 0
for ($i=0;$i<$n;$i++) {//Enter the loop
if(eregi($found,$list[$i]) ){//The entered keywords are matched and compared with the strings in the array $list[$i]
$row=explode("|",$list[$i]); $id=1; // If a match is found, use "|" as the delimiter to cut the message information $list[$i] (the $ith message), and assign these data to the array $row. And assign 1 to the variable $id so as to Determine whether a match is found.
list($ip,$datetime,$name,$email,$home,$face,$qq,$head,$text,$reply)=$row;//Change the array $ The unit data in the row is assigned to the variables in the brackets in order
?>
>//Display the customer image picture


Nickname【
//Display customer name
Posted in ://Display the message publishing time


>//Display the customer message emoticon picture
Say://Show customer

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532670.htmlTechArticleSource: http://www.pchoer.com/ PHP realizes data display, addition and modification of text database I use a guestbook program as an example to explain the five basic operations of , deletion and query...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template