«
PHP新手上路(9)

时间:2008-5-31    作者:Deri    分类: 分享


   <p>  建设一个简单交互的网站(五) </p><p>  8. 投票系统 </p><p>  在许多时候,我们需要收集上网者和网友们的意见。例如:新版页面与旧版页面的比较;对某一事情的看法;对体育比赛结果的预测等等。这时候,你需要一个非常有效的网上调查系统。使用PHP就可以非常方便地实现你的这一构想。 </p><p>  8.1 投票系统(mypolls.php3): </p><p>  <code><?<br />$status=0;<br />if(isset($polled)&&($polled=="c-e")){<br />$status=1;<br />}<br />#echo "$status";<br />if(isset($poll)&&($status==0)){<br />setcookie("polled","c-e",time()+86400,"/");#time=24h<br />}<br />?><br /><html><br /><head><br /><title>新版页面调查</title><br /><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><br /><style type="text/css"><br /><!--<br />.tb { border="1" bordercolor="#009933" cellspacing="0" font-size: 9pt; color: #000000}<br />.head { font-family: "宋体"; font-size: 12pt; font-weight: bold; color: #009933; text-decoration: none}<br />.pt9 { font-size: 9pt}<br />a.p9:link { font-size: 9pt; color: #000000; text-decoration: none}<br />a.p9:visited { font-size: 9pt; color: #000000; text-decoration: none }<br />a.p9:hover { font-size: 9pt; color: #FF0000; text-decoration: underline}<br />a.p9:active { font-size: 9pt; color: #FF0000; text-decoration: underline }<br />--><br /></style><br /></head><br /><body bgcolor="#FFFFFF"><br /><div class="head">与旧版页面相比较您觉得新版页面:</div><br><br /><?<br />if(!isset($submit)){<br />?><br /><form action="myPolls.php3" method="get"><br /><input type="radio" name="poll_voteNr" value="1" checked ><br /><span class="pt9">信息量更大</span> <br><br /><input type="radio" name="poll_voteNr" value="2" ><br /><span class="pt9">网页更精美</span> <br><br /><input type="radio" name="poll_voteNr" value="3" ><br /><span class="pt9">没什么改进</span> <br><br /><input type="radio" name="poll_voteNr" value="4" ><br /><span class="pt9">其它</span> <br><br /><input type="submit" name="submit" value="OK"><br /><input type="hidden" name="poll" value="vote"><br /><A HREF="myPolls.php3?submit=OK" class="p9">查看调查结果</A><br /></form><br /><?<br />/*<br />如果想增加其它的选项可直接加上即可<br />*/<br />}else{<br />$descArray=array(1=>"信息量更大",<br />2=>"网页更精美",<br />3=>"没什么改进",<br />4=>"其它"<br />);<br />$poll_resultBarHeight = 9; // height in pixels of percentage bar in result table<br />$poll_resultBarScale = 1; // scale of result bar (in multiples of 100 pixels)<br />$poll_tableHeader="<table border=1 class="tb">";<br />$poll_rowHeader="<tr>";<br />$poll_dataHeader="<td align=center>";<br />$poll_dataFooter="</td>";<br />$poll_rowFooter="</tr>";<br />$poll_tableFooter="</table>";<br />$coutfile="data.pol";<br />$poll_sum=0;<br />// read counter-file<br />if (file_exists( $coutfile))<br />{<br />$fp = fopen( $coutfile, "rt");<br />while ($Line = fgets($fp, 10))<br />{<br />// split lines into identifier/counter<br />if (ereg( "([^ ]*) *([0-9]*)", $Line, $tmp))<br />{<br />$curArray[(int)$tmp[1]] = (int)$tmp[2];<br />$poll_sum+=(int)$tmp[2];<br />}<br />}<br />// close file<br />fclose($fp);<br />}else{//<br />for ($i=1;$i<=count($descArray);$i++){<br />$curArray[$i]=0;<br />}<br />}<br />if(isset($poll)){<br />$curArray[$poll_voteNr]++;<br />$poll_sum++;<br />}<br />echo $poll_tableHeader;<br />// cycle through all options编历数组<br />reset($curArray);<br />while (list($K, $V) = each($curArray))<br />{<br />$poll_optionText = $descArray[$K];<br />$poll_optionCount = $V;<br />echo $poll_rowHeader;<br />if($poll_optionText != "")<br />{<br />echo $poll_dataHeader;<br />echo $poll_optionText;<br />echo $poll_dataFooter;<br />if($poll_sum)<br />$poll_percent = 100 * $poll_optionCount / $poll_sum;<br />else<br />$poll_percent = 0;<br />echo $poll_dataHeader;<br />if ($poll_percent > 0)<br />{<br />$poll_percentScale = (int)($poll_percent * $poll_resultBarScale);<br />}<br />printf(" %.2f %% (%d)", $poll_percent, $poll_optionCount);<br />echo $poll_dataFooter;<br />}<br />echo $poll_rowFooter;<br />}<br />echo "总共投票次数:<font color=red> $poll_sum</font>";<br />echo $poll_tableFooter;<br />echo "<br>";<br />echo "<input type="submit" name="Submit1" value="返回主页" onClick="javascript:location='http://gophp.heha.net/index.html'">";<br />echo " <input type="submit" name="Submit2" value="重新投票" onClick="javascript:location='http://gophp.heha.net/mypolls.php3'">";<br />if(isset($poll)){<br />// write counter file<br />$fp = fopen($coutfile, "wt");<br />reset($curArray);<br />while (list($Key, $Value) = each($curArray))<br />{<br />$tmp = sprintf( "%s %dn", $Key, $Value);<br />fwrite($fp, $tmp);<br />}<br />// close file<br />fclose($fp);<br />}<br />}<br />?><br /></body><br /></html></code></p>
<p> </p>

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