Example demonstration of common operation methods of text database realized by PHP

  • 2021-07-06 10:39:44
  • OfStack

PHP can realize five basic operations such as displaying, adding, modifying, deleting and querying the data of text database.
Taking a message book program as an example, we briefly describe the five basic operations of PHP under 1 to display, add, modify, delete and query the data of the text database.

There are 10 fields in this text database: customer IP, speaking time, customer name, customer EMAIL, customer homepage address, message expression picture name, customer QQ, customer image picture, message content and administrator reply content.

1. Add data program:


$date=date("Y-m-d H:i:s");// Get system time 
$ip = $HTTP_SERVER_VARS[REMOTE_ADDR]; // Having obtained a speech IP Address 
$text=encode($gb_text);// Remove the space after the message content .
$fp=fopen("gb.dat","a");// Open in write-only mode gb.dat Text file , The file pointer points to the end of the file .
$str=$ip."|".$date."|".$gb_name."|".$gb_email."|".$gb_home."|".$face."|".$gb_qq."|".$head."|".$text."|".$reply." ";// Assign the data of all messages to variables $str , "|" The purpose is to use data interval symbols for future data segmentation. 
fwrite($fp,$str);// Write data to a file 
fclose($fp);// Close a file 
showmessage(" Message Successful !","index.php","3");// The message was successful, 3 Automatically return to the main interface after seconds. 

Where $gb_name, $gb_email, $gb_home, $face, $gb_qq, $head, $gb_text, and $reply are data passed from the speech form.

2. Display data program:


<?
if (file_exists("gb.dat")){//检测文件是否存在
$array=file("gb.dat");//将文件全部内容读入到数组$array
$arr=array_reverse($array);//将$array里的数据安行翻转排列(即最后1行当第1行,依此类推)读入数组$arr的每1个单元($arr[0]...)。
$num=count($array);//获取数组$array里的信息数(1行为1条信息)
if ($num>0){//如果信息数大于零(即文本数据库不为空)
$total=ceil($num/$pagesize);//计算总页数(取最大整数,即凡有小数点都进1取整,$pagesize为预设的每页显示的信息数)
if($page<1){//如果当前页面数码号小于1
$page=1;//则赋值为1
}
$number=($page-1)*$pagesize;//计算当前所显示第1个留言的数码号(数码号从零开始,主要是达到与数组单元号对应的目的)
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里的单元数据按顺序赋予括号里的变量
?>
<img src=<? echo $head ?> >//显示客户形象图片
<br>
<font color="#0099CC">昵称【<? echo $name ?><font size="2">】<br>//显示客户名
发表于:<? echo $datetime ?>//显示留言发表时间
<br> 
<img src=<? echo $face ?>>//显示客户留言表情图片
<? echo $name ?>说:<? echo $text; ?>//显示客户留言内容 
<br> 
<? echo $reply ?>//显示回复内容
<br>
<a href="<? echo $home ?>" rel="external nofollow" target="_blank">访问<? echo $name ?>的主页</a>//客户主页的超连接
<a href="mailto:<? echo $email ?>" rel="external nofollow" >给<? echo $name ?>发信</a>//客户E-MAIL的连接
<? echo $name ?>的QQ号码是<? echo $qq ?>//显示客户的QQ号码 
<? echo $name ?>的IP地址为<? echo $ip ?>" //显示客户的IP地址
<a href="reply.php?time=<? echo $datetime ?>" rel="external nofollow" >回复</a>//留言回复的连接语句 
<a href="del.php?time=<? echo $datetime ?>" rel="external nofollow" >删除</a>//留言删除的语句(以客户留言时间$datetime作为删除标识)
<br> 
<? 
if ($number == $num-1)//如果数组的单元号等于总留言数减1(因为单元号以零开始的,所以这意味着这是最后1条留言)
{
break;//跳出循环
}
$number = $number + 1; //数组单元号加1
}//循环结束符
}
if ($page <> 1)//如果当前页面数码号不等于1
{
$back = $page - 1;//当前页面数码号减1,并将此值赋予变量$back
echo "<a href=index.php?page=1>第1页</a>";//显示第1页的连接
echo " <a href=index.php?page=$back>上1页</a>" ;当前页面数码号等于$back,并显示其连接
}
if ($page <> $total)//如果当前页面数码号不等于总页数号(最后1页数码号)
{
$next = $page + 1;//当前页面数码号加1并赋予变量$next
echo " <a href=index.php?page=$next>下1页</a>" ;//显示下1页连接
echo " <a href=index.php?page=$total>最后1页</a>"; 显示最后1页连接
} 
echo "页数:$page / $total";//显示当前页面数码号和显示最后1页数码号
echo "共有 $num 条留言";//显示留言数信息
}
else {
echo "<center>当前没有任何留言!</center>";//如果文件内容为空则显示的信息
}
else {
echo "<center>数据文件丢失,请联系管理员!或发布留言重新建立数据文件!</center>";//如果文件不存在显示的信息
}

3. Data modification procedure:


$list=file("gb.dat");// Read the entire gb.dat File to Array $list, Array per 1 Elements are 1 A message ($list[0] Is the first 1 The data of the message, $list[1] Is the first 1 Data of a message .....
$n=count($list);// Calculation $list Total number of messages in the content , And assign variables $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 cycle 
if(eregi($ttime,$list[$i])){// Will send the message time $ttime And array unit $list Compare the contents of the 
$f=explode("|",$list[$i]);// If a match is found, the "|" As a separator , Cut open the message $list[$i]( No. 1 $i A message ), And assign that data to an array $f
$f[9]=$gb_reply;// Will $f[9] (At the end of the message 1 Bar data) for $gb_reply (Reply) instead.  
$list[$i]=$f[0]."|".$f[1]."|".$f[2]."|".$f[3]."|".$f[4]."|".$f[5]."|".$f[6]."|".$f[7]."|".$f[8]."|".$f[9]." "; // Set array cells $list[$i] Array with the contents of $f Add a separator "|" Replace (in which $f[9] Is the modified new data). 
break;// Out of the loop 
}
}// Loop terminator 
}
FOR($i=0;$i<=$n;$i++){// Enter the cycle 
fwrite($fp,$list[$i]);// Set the array $list Each unit of is 1 Line, write to file gb.dat
}// Loop terminator  
fclose($fp);// Close a file 
showmessage(" Successful reply !","index.php");// If the reply is successful, it will automatically return to the main interface. 

4. Data deletion program:


$list=file("gb.dat");// Read the entire gb.dat File to Array $list, Array per 1 Elements are 1 A message ($list[0] Is the first 1 The data of the message, $list[1] Is the first 1 Data of a message .....
$n=count($list);// Calculation $list Total number of messages in the content , And assign variables $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 cycle 
if(eregi($ttime,$list[$i])){// Will be sent to send a message time $ttime And arrays $list[$i] To match and compare the strings in  
$list[$i]="";// If the match is successful, the $list[$i] Empty (for the purpose of deletion) 
break;// Out of the loop 
}
}// Loop terminator  
FOR($i=0;$i<=$n;$i++){// Enter the cycle 
fwrite($fp,$list[$i]);// Set the array $list Each unit of is 1 Line, write to file gb.dat
} // Loop terminator 
fclose($fp);// Close a file 
showmessage(" Delete succeeded !","index.php");// Delete successfully, automatically return to the main interface. 
}

5. Data query program:


<form action="search.php" method="post">
<font color="#0099CC" size="2"> Search keywords:  
<input name="found" type="text" id="found" style="background-color:#FFFFFF; color:#8888AA; border: 1 double #3399CC" size="12">
<input name="submit" type="image" src="image/search.gif" alt=" Message search ">
</font></td>
</tr>
</table>
</form>
//////////////////////////////// The above is the search form statement section 
<?
$id=0;
$list=file("gb.dat");// Read the entire gb.dat File to Array $list, Array per 1 Elements are 1 A message ($list[0] Is the first 1 The data of the message, $list[1] Is the first 1 Data of a message .....
$n=count($list);// Calculation $list Total number of messages in the content , And assign variables $n
$found=trim($found);
if (!$found){ // If $found Be false 
echo "<center> You have not entered any keywords! </center>";// Make relevant display 
}
else {
if($n>0){// If the number of messages is greater than 0
for ($i=0;$i<$n;$i++) {// Enter the cycle 
if(eregi($found,$list[$i])){// Input keywords and arrays $list[$i] To match and compare the strings in 
$row=explode("|",$list[$i]); $id=1; // If a match is found, the "|" As a separator , Cut open the message $list[$i]( No. 1 $i A message ), And assign that data to an array $row. And set the variable $id Endowed with 1 So as to judge whether a match has been found. 
list($ip,$datetime,$name,$email,$home,$face,$qq,$head,$text,$reply)=$row;// Set the array $row The unit data in the parenthesis is assigned to the variables in brackets in sequence 
?>
<img src=<? echo $head ?> >// Display customer image pictures 
<br>
<font color="#0099CC"> Nickname " <? echo $name ?><font size="2"> " <br>// Display customer name 
 Published in: <? echo $datetime ?>// Display message posting time 
<br> 
<img src=<? echo $face ?>>// Display customer message emoticons 
<? echo $name ?> Say :<? echo $text; ?>// Show customers 

Related articles: