


来源: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$page=1;//则赋值为1
}
$number=($page-1)*$pagesize;//计算当前所显示第一个留言的数码号(数码号从零开始,主要是达到与数组单元号对应的目的)
for($i=0;$i$row=explode("|",$arr[$number]);//以"|"作为分割符,分割数组$arr中每第$number个单元的数据,并将这些数据赋予数组$rom
list($ip,$datetime,$name,$email,$home,$face,$qq,$head,$text,$reply)=$row;//将数组$row里的单元数据按顺序赋予括号里的变量
?> >//显示客户形象图片
昵称【 echo $name ?>】
//显示客户名
发表于: echo $datetime ?>//显示留言发表时间
>//显示客户留言表情图片
echo $name ?>说: echo $text; ?>//显示客户留言内容
echo $reply ?>//显示回复内容
访问 echo $name ?>的主页//客户主页的超连接
给 echo $name ?>发信//客户E-MAIL的连接
echo $name ?>的QQ号码是 echo $qq ?>//显示客户的QQ号码
echo $name ?>的IP地址为 echo $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;$iif(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;$ifwrite($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;$iif(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;$ifwrite($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 "
}
else {
if($n>0 ){//If the number of messages is greater than 0
for ($i=0;$iif(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
?>
Nickname【 echo $name ?>】
//Display customer name
Posted in : echo $datetime ?>//Display the message publishing time
echo $name ?>Say: echo $text; ?>//Show customer

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
