Discuz教程网

PHP新手上路 - 009 - 投票系统

[复制链接]
authicon php1314 发表于 2010-12-5 10:40:40 | 显示全部楼层 |阅读模式
建设一个简单交互的网站(五)
8. 投票系统
  在许多时候,我们需要收集上网者和网友们的意见。例如:新版页面与旧版页面的比较;对某一事情的看法;对体育比赛结果的预测等等。这时候,你需要一个非常有效的网上调查系统。使用PHP就可以非常方便地实帜愕恼庖还瓜搿?
8.1 投票系统(mypolls.php3):

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

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



上一篇:PHP新手上路 - 008 - 文件上传
下一篇:PHP新手上路 - 010 - 简易banner动态更替
authicon 纤陌陌 发表于 2011-5-10 16:59:56 | 显示全部楼层
看帖必回
authicon 82xiaochong911 发表于 2011-5-11 12:59:33 | 显示全部楼层
不错,我喜欢
authicon forever21 发表于 2011-5-15 12:59:32 | 显示全部楼层
强人,佩服死了。
authicon huanglv80 发表于 2011-5-19 21:59:36 | 显示全部楼层
看一下啊,嘻嘻
authicon fhiejkl 发表于 2011-5-21 20:59:39 | 显示全部楼层
真的有意思!
authicon 82xiaochong911 发表于 2011-5-26 22:59:38 | 显示全部楼层
好东东下下来看看
authicon 福倒菜菜子 发表于 2011-6-19 09:59:59 | 显示全部楼层
这个不错呀
authicon lanyuqing 发表于 2011-6-26 07:59:41 | 显示全部楼层
哈哈  收了你
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 07:49

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

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