海若有因
VX:white396
这篇文章是根据《PHP动态网站开发实例教程》所学课程所编译,运行与腾讯云开发者平台
(注:腾讯云开发者平台只能用于简单的编译,不能调用JS,数据库)
若有看不懂的请私信或加vx了解
本章节任务称述
**掌握文件处理技术对于web开发者来说十分重要的。
虽然处理信息方面
使用数据库是多数情况下的选择
但对于少量的数据
利用文件夹存取是非常方便,快捷的
更关键的是,PHP中提供了非常简单,方便的文件,目录处理方法**
代码如下
<html>
<head>
<meta http-cquiv="Content-Tvpe"content-"text/html;charset=UTF-8">
<h2 align="center" class="STYLE1">喜欢电影投票统计</h2>
<title>投票统计</title>
<style type="text/css">
<!--
.STYLE1 {color: #0099FF}
-->
</style>
</head>
<body>
<form action=""method="post">
<table align="center" bordercolor="#00CC99" bgcolor="#0099CC">
<tr><td><b>你最喜欢的电影:</b></td></tr>
<tr><td><input type="radio"name="vote"value="泰坦尼克号">泰坦尼克号</td></tr>
<tr><td><input type="radio"name="vote"value="肖生克的救赎">肖生克的救赎</td/tr>
<<tr><td><input type="radio"name="vote"value="霸王别姬">霸王别姬</td></tr>
</tr>
<tr><td><input type="submit"name="bt"value="我要投票"></td></tr>
</table>
</form>
<div align="center">
<?php
$votefile="vote.txt";
if(!file_exists($votefile)){
$handle=fopen($votefile,"w+");
fwrite($handle,"0|0|0");
fclose($handle);
}
if(isset($_POST['bt'])) {
if(isset($_POST['vote'])) {
$vote=$_POST['vote'];
$handle=fopen($votefile,"r+");
$votestr=fread($handle,filesize($votefile));
fclose($handle);
$votearray=explode("|",$votestr);
echo"<h3>投票完毕</h3>";
if($vote=='泰坦尼克号')
$votearray[0]++;
if($vote=='肖生克的救赎')
$votearray[1]++;
if($vote=='霸王别姬')
$votearray[2]++;
echo"目前泰坦尼克号的支持票数为:".$votearray[0]."<br>";
echo"目前肖生克的救赎的支持票数为:".$votearray[1]."<br>";
echo"目前霸王别姬的支持票数为:".$votearray[2]."<br>";
$sum=$votearray[0]+$votearray[1]+$votearray[2];
echo"总票数为:".$sum."<br>";
$votestr2=implode("|",$votearray);
$handle=fopen($votefile,"w+");
fwrite($handle,$votestr2);
fclose($handle);
}
else{
echo"<script>alert('未选择投票选项.!')</script>";
}}
?>
</div>
<h4 align="center" class="STYLE1">版权声明:此文本由海若有因独立创作,转发请留言\(^o^)/~</h4>
</body>
</html>
点击右边菜单栏中重新部署,等待部署完成后点击测试域名弹出网页就可以看到投票结果啦