PHP新手上路(九)

建设一个简单交互的网站(五)

8. 投票系统

在许多时候,我们需要收集上网者和网友们的意见。例如:新版页面与旧版页面的比较;对某一事情的看法;对体育比赛结果的预测等等。这时候,你需要一个非常有效的网上调查系统。使用PHP就可以非常方便地实现你的这一构想。

8.1 投票系统(mypolls.php3):

  1<html>
  2<head>
  3<title>新版页面调查</title>
  4<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
  5<style type="text/css">   
  6<!--   
  7.tb { border="1" bordercolor="#009933" cellspacing="0" font-size: 9pt; color: #000000}   
  8.head { font-family: "宋体"; font-size: 12pt; font-weight: bold; color: #009933; text-decoration: none}   
  9.pt9 { font-size: 9pt}   
 10a.p9:link { font-size: 9pt; color: #000000; text-decoration: none}   
 11a.p9:visited { font-size: 9pt; color: #000000; text-decoration: none }   
 12a.p9:hover { font-size: 9pt; color: #FF0000; text-decoration: underline}   
 13a.p9:active { font-size: 9pt; color: #FF0000; text-decoration: underline }   
 14\-->   
 15</style>
 16</head>
 17<body bgcolor="#FFFFFF">
 18<div class="head">与旧版页面相比较您觉得新版页面:</div><br/>
 19<?   
 20if(!isset($submit)){   
 21?>
 22<form action="myPolls.php3" method="get">
 23<input checked="" name="poll_voteNr" type="radio" value="1"/>
 24<span class="pt9">信息量更大</span> <br/>
 25<input name="poll_voteNr" type="radio" value="2"/>
 26<span class="pt9">网页更精美</span> <br/>
 27<input name="poll_voteNr" type="radio" value="3"/>
 28<span class="pt9">没什么改进</span> <br/>
 29<input name="poll_voteNr" type="radio" value="4"/>
 30<span class="pt9">其它</span> <br/>
 31<input name="submit" type="submit" value="OK"/>
 32<input name="poll" type="hidden" value="vote"/>
 33<a class="p9" href="myPolls.php3?submit=OK">查看调查结果</a>
 34</form>
 35<?   
 36/*   
 37如果想增加其它的选项可直接加上即可   
 38*/   
 39}else{   
 40$descArray=array(1=>"信息量更大",   
 412=&gt;"网页更精美",   
 423=&gt;"没什么改进",   
 434=&gt;"其它"   
 44);   
 45$poll_resultBarHeight = 9; // height in pixels of percentage bar in result table   
 46$poll_resultBarScale = 1; // scale of result bar (in multiples of 100 pixels)   
 47$poll_tableHeader="<table border="1" class="tb">";   
 48$poll_rowHeader="<tr>";   
 49$poll_dataHeader="<td align="center">";   
 50$poll_dataFooter="</td>";   
 51$poll_rowFooter="</tr>";   
 52$poll_tableFooter="</table>";   
 53$coutfile="data.pol";   
 54$poll_sum=0;   
 55  
 56// read counter-file   
 57if (file_exists( $coutfile))   
 58{   
 59$fp = fopen( $coutfile, "rt");   
 60while ($Line = fgets($fp, 10))   
 61{   
 62// split lines into identifier/counter   
 63if (ereg( "([^ ]*) *([0-9]*)", $Line, $tmp))   
 64{   
 65$curArray[(int)$tmp[1]] = (int)$tmp[2];   
 66$poll_sum+=(int)$tmp[2];   
 67}   
 68}   
 69// close file   
 70fclose($fp);   
 71}else{//   
 72for ($i=1;$i&lt;=count($descArray);$i++){   
 73$curArray[$i]=0;   
 74}   
 75}   
 76if(isset($poll)){   
 77$curArray[$poll_voteNr]++;   
 78$poll_sum++;   
 79}   
 80echo $poll_tableHeader;   
 81  
 82// cycle through all options编历数组   
 83reset($curArray);   
 84while (list($K, $V) = each($curArray))   
 85{   
 86$poll_optionText = $descArray[$K];   
 87$poll_optionCount = $V;   
 88echo $poll_rowHeader;   
 89  
 90if($poll_optionText != "")   
 91{   
 92echo $poll_dataHeader;   
 93echo $poll_optionText;   
 94echo $poll_dataFooter;   
 95  
 96if($poll_sum)   
 97$poll_percent = 100 * $poll_optionCount / $poll_sum;   
 98else   
 99$poll_percent = 0;   
100echo $poll_dataHeader;   
101  
102if ($poll_percent &gt; 0)   
103{   
104$poll_percentScale = (int)($poll_percent * $poll_resultBarScale);   
105}   
106  
107printf(" %.2f %% (%d)", $poll_percent, $poll_optionCount);   
108  
109echo $poll_dataFooter;   
110}   
111  
112echo $poll_rowFooter;   
113}   
114  
115echo "总共投票次数:<font color="red"> $poll_sum</font>";   
116echo $poll_tableFooter;   
117echo "<br/>";   
118echo "<input name="Submit1" onclick="javascript:location='http://gophp.heha.net/index.html'" type="submit" value="返回主页"/>";   
119echo " <input name="Submit2" onclick="javascript:location='http://gophp.heha.net/mypolls.php3'" type="submit" value="重新投票"/>";   
120if(isset($poll)){   
121// write counter file   
122$fp = fopen($coutfile, "wt");   
123reset($curArray);   
124while (list($Key, $Value) = each($curArray))   
125{   
126$tmp = sprintf( "%s %dn", $Key, $Value);   
127fwrite($fp, $tmp);   
128}   
129// close file   
130fclose($fp);   
131}   
132}   
133?&gt;   
134</body>
135</html>

注释:从上面我们可以看出该投票系统的基本过程:
1、打开文件取得数据到数组$curArray(文件不存在则初始化数组$curArray)
2、编历数组,处理数据得到所需值
3、计算百分比,控制统计bar图像宽度
4、将数据保存到"data.pol"中

这里有一点是需要注意:这里的data.pol文本文件需要有写权限。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus