PHP新手上路(九)
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-05-24
n=center>"; $poll_dataFooter="</td>"; $poll_rowFooter="</tr>"; $poll_tableFooter="</table>"; $coutfile="data.pol"; $poll_sum=0; // read counter-file if (file_exists( $coutfile)) { $fp = fopen( $coutfile, "rt"); while ($Line = fgets($fp, 10)) { // split lines into identifier/counter if (ereg( "([^ ]*) *([0-9]*)", $Line, $tmp)) { $curArray[(int)$tmp[1]] = (int)$tmp[2]; $poll_sum+=(int)$tmp[2]; } } // close file fclose($fp); }else{// for ($i=1;$i<=count($descArray);$i++){ $curArray[$i]=0; } } if(isset($poll)){ $curArray[$poll_voteNr]++; $poll_sum++; } echo $poll_tableHeader; // cycle through all options编历数组 reset($curArray); while (list($K, $V) = each($curArray)) { $poll_optionText = $descArray[$K]; $poll_optionCount = $V; echo $poll_rowHeader; if($poll_optionText != "") { echo $poll_dataHeader; echo $poll_optionText; echo $poll_dataFooter; if($poll_sum) $poll_percent = 100 * $poll_optionCount / $poll_sum; else $poll_percent = 0; echo $poll_dataHeader; if ($poll_percent > 0) { $poll_percentScale = (int)($poll_percent * $poll_resultBarScale); } printf(" %.2f %% (%d)", $poll_percent, $poll_optionCount); echo $poll_dataFooter; } echo $poll_rowFooter; } echo "总共投票次数:<font color=red> $poll_sum</font>"; echo $poll_tableFooter; echo "<br>"; echo "<input type="submit" name="Submit1" value="返回主页" onClick="javascript:location=''http://gophp.heha.net/index.html''">"; echo " <input type="submit" name="Submit2" value="重新投票" onClick="javascript:location=''http://gophp.heha.net/mypolls.php3''">"; if(isset($poll)){ // write counter file $fp = fopen($coutfile, "wt"); reset($curArray); while (list($Key, $Value) = each($curArray)) { $tmp = sprintf( "%s %dn", $Key, $Value); fwrite($fp, $tmp); } // close file fclose($fp); } } ?> </body> </html> 注释:从上面我们可以看出该投票系统的基本过程: 1、打开文件取得数据到数组$curArray(文件不存在则初始化数组$curArray) 2、编历数组,处理数据得到所需值 3、计算百分比,控制统计bar图像宽度 4、将数据保存到"data.pol"中 这里有一点是需要注意:这里的data.pol文本文件需要有写权限。 |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
上一篇: php 面向对象(二)下一篇: php面向对象(三)
关于PHP新手上路(九)的所有评论