设为首页收藏本站language 语言切换
查看: 1246|回复: 0
收起左侧

PHP新手上路(九)

[复制链接]
发表于 2010-2-24 13:38:15 | 显示全部楼层 |阅读模式
8. 投票系统 <BR><BR>  在许多时候,我们需要收集上网者和网友们的意见。例如:新版页面与旧版页面的比较;对某一事情的看法;对体育比赛结果的预测等等。这时候,你需要一个非常有效的网上调查系统。使用PHP就可以非常方便地实现你的这一构想。 <BR><BR>8.1 投票系统(mypolls.php3): <BR><BR>&lt;? <BR>$status=0; <BR>if(isset($polled)&amp;&amp;($polled=="c-e")){ <BR>$status=1; <BR>} <BR>#echo "$status"; <BR>if(isset($poll)&amp;&amp;($status==0)){ <BR>setcookie("polled","c-e",time()+86400,"/");#time=24h <BR>} <BR>?&gt; <BR>&lt;html&gt; <BR>&lt;head&gt; <BR>&lt;title&gt;新版页面调查&lt;/title&gt; <BR>&lt;meta http-equiv="Content-Type" content="text/html; charset=gb2312"&gt; <BR>&lt;style type="text/css"&gt; <BR>&lt;!-- <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>--&gt; <BR>&lt;/style&gt; <BR>&lt;/head&gt; <BR>&lt;body bgcolor="#FFFFFF"&gt; <BR>&lt;div class="head"&gt;与旧版页面相比较您觉得新版页面:&lt;/div&gt;&lt;br&gt; <BR>&lt;? <BR>if(!isset($submit)){ <BR>?&gt; <BR>&lt;form action="myPolls.php3" method="get"&gt; <BR>&lt;input type="radio" name="poll_voteNr" value="1" checked &gt; <BR>&lt;span class="pt9"&gt;信息量更大&lt;/span&gt; &lt;br&gt; <BR>&lt;input type="radio" name="poll_voteNr" value="2" &gt; <BR>&lt;span class="pt9"&gt;网页更精美&lt;/span&gt; &lt;br&gt; <BR>&lt;input type="radio" name="poll_voteNr" value="3" &gt; <BR>&lt;span class="pt9"&gt;没什么改进&lt;/span&gt; &lt;br&gt; <BR>&lt;input type="radio" name="poll_voteNr" value="4" &gt; <BR>&lt;span class="pt9"&gt;其它&lt;/span&gt; &lt;br&gt; <BR>&lt;input type="submit" name="submit" value="OK"&gt; <BR>&lt;input type="hidden" name="poll" value="vote"&gt; <BR>&lt;A HREF="myPolls.php3?submit=OK" class="p9"&gt;查看调查结果&lt;/A&gt; <BR>&lt;/form&gt; <BR>&lt;? <BR>/* <BR>如果想增加其它的选项可直接加上即可 <BR>*/ <BR>}else{ <BR>$descArray=array(1=&gt;"信息量更大", <BR>2=&gt;"网页更精美", <BR>3=&gt;"没什么改进", <BR>4=&gt;"其它" <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="&lt;table border=1 class="tb"&gt;"; <BR>$poll_rowHeader="&lt;tr&gt;"; <BR>$poll_dataHeader="&lt;td align=center&gt;"; <BR>$poll_dataFooter="&lt;/td&gt;"; <BR>$poll_rowFooter="&lt;/tr&gt;"; <BR>$poll_tableFooter="&lt;/table&gt;"; <BR>$coutfile="data.pol"; <BR>$poll_sum=0; <BR><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&lt;=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><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><BR>if($poll_optionText != "") <BR>{ <BR>echo $poll_dataHeader; <BR>echo $poll_optionText; <BR>echo $poll_dataFooter; <BR><BR>if($poll_sum) <BR>$poll_percent = 100 * $poll_optionCount / $poll_sum; <BR>else <BR>$poll_percent = 0; <BR>echo $poll_dataHeader; <BR><BR>if ($poll_percent &gt; 0) <BR>{ <BR>$poll_percentScale = (int)($poll_percent * $poll_resultBarScale); <BR>} <BR><BR>printf(" %.2f %% (%d)", $poll_percent, $poll_optionCount); <BR><BR>echo $poll_dataFooter; <BR>} <BR><BR>echo $poll_rowFooter; <BR>} <BR><BR>echo "总共投票次数:&lt;font color=red&gt; $poll_sum&lt;/font&gt;"; <BR>echo $poll_tableFooter; <BR>echo "&lt;br&gt;"; <BR>echo "&lt;input type="submit" name="Submit1" value="返回主页" onClick="javascript:location='http://gophp.heha.net/index.html'"&gt;"; <BR>echo " &lt;input type="submit" name="Submit2" value="重新投票" onClick="javascript:location='http://gophp.heha.net/mypolls.php3'"&gt;"; <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>?&gt; <BR>&lt;/body&gt; <BR>&lt;/html&gt; <BR><BR>注释:从上面我们可以看出该投票系统的基本过程: <BR>1、打开文件取得数据到数组$curArray(文件不存在则初始化数组$curArray) <BR>2、编历数组,处理数据得到所需值 <BR>3、计算百分比,控制统计bar图像宽度 <BR>4、将数据保存到"data.pol"中
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-4-17 03:35 , Processed in 0.776655 second(s), 24 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

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