Shanghai WTO Forum

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5628|回复: 0

PHP新手上路(九)

[复制链接]
发表于 2007-12-3 17:53:18 | 显示全部楼层 |阅读模式
建设一个简单交互的网站(五) <></P><>8. 投票系统 </P><>  在许多时候,我们需要收集上网者和网友们的意见。例如:新版页面与旧版页面的比较;对某一事情的看法;对体育比赛结果的预测等等。这时候,你需要一个非常有效的网上调查系统。使用PHP就可以非常方便地实现你的这一构想。 </P><>8.1 投票系统(mypolls.php3): </P><><? <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; </P><>// 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; </P><>// 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; </P><>if($poll_optionText != "") <BR>{ <BR>echo $poll_dataHeader; <BR>echo $poll_optionText; <BR>echo $poll_dataFooter; </P><>if($poll_sum) <BR>$poll_percent = 100 * $poll_optionCount / $poll_sum; <BR>else <BR>$poll_percent = 0; <BR>echo $poll_dataHeader; </P><>if ($poll_percent > 0) <BR>{ <BR>$poll_percentScale = (int)($poll_percent * $poll_resultBarScale); <BR>} </P><P>printf(" %.2f %% (%d)", $poll_percent, $poll_optionCount); </P><P>echo $poll_dataFooter; <BR>} </P><P>echo $poll_rowFooter; <BR>} </P><P>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> </P><P>注释:从上面我们可以看出该投票系统的基本过程: <BR>1、打开文件取得数据到数组$curArray(文件不存在则初始化数组$curArray) <BR>2、编历数组,处理数据得到所需值 <BR>3、计算百分比,控制统计bar图像宽度 <BR>4、将数据保存到"data.pol"中 </P><P>   这里有一点是需要注意:这里的data.pol文本文件需要有写权限。</P>
您需要登录后才可以回帖 登录 | 注册

本版积分规则


QQ|Archiver|mobile|The little black house|Shanghai WTO Net ( 沪ICP备10034107号-3 )

GMT+8, 2024-12-22 18:06

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表