欢迎光临
我们一直在努力

PHP新手上路(九)-PHP教程,PHP基础

建站超值云服务器,限时71元/月

8. 投票系统

  在许多时候,我们需要收集上网者和网友们的意见。例如:新版页面与旧版页面的比较;对某一事情的看法;对体育比赛结果的预测等等。这时候,你需要一个非常有效的网上调查系统。使用php就可以非常方便地实现你的这一构想。

8.1 投票系统(mypolls.php3):

<?

$status=0;

if(isset($polled)&&($polled=="c-e")){

$status=1;

}

#echo "$status";

if(isset($poll)&&($status==0)){

setcookie("polled","c-e",time()+86400,"/");#time=24h

}

?>

<html>

<head>

<title>新版页面调查</title>

<meta http-equiv="content-type" content="text/html; charset=gb2312">

<style type="text/css">

<!–

.tb { border="1" bordercolor="#009933" cellspacing="0" font-size: 9pt; color: #000000}

.head { font-family: "宋体"; font-size: 12pt; font-weight: bold; color: #009933; text-decoration: none}

.pt9 { font-size: 9pt}

a.p9:link { font-size: 9pt; color: #000000; text-decoration: none}

a.p9:visited { font-size: 9pt; color: #000000; text-decoration: none }

a.p9:hover { font-size: 9pt; color: #ff0000; text-decoration: underline}

a.p9:active { font-size: 9pt; color: #ff0000; text-decoration: underline }

–>

</style>

</head>

<body bgcolor="#ffffff">

<div class="head">与旧版页面相比较您觉得新版页面:</div><br>

<?

if(!isset($submit)){

?>

<form action="mypolls.php3" method="get">

<input type="radio" name="poll_votenr" value="1" checked >

<span class="pt9">信息量更大</span> <br>

<input type="radio" name="poll_votenr" value="2" >

<span class="pt9">网页更精美</span> <br>

<input type="radio" name="poll_votenr" value="3" >

<span class="pt9">没什么改进</span> <br>

<input type="radio" name="poll_votenr" value="4" >

<span class="pt9">其它</span> <br>

<input type="submit" name="submit" value="ok">

<input type="hidden" name="poll" value="vote">

<a href="mypolls.php3?submit=ok" class="p9">查看调查结果</a>

</form>

<?

/*

如果想增加其它的选项可直接加上即可

*/

}else{

$descarray=array(1=>"信息量更大",

2=>"网页更精美",

3=>"没什么改进",

4=>"其它"

);

$poll_resultbarheight = 9; // height in pixels of percentage bar in result table

$poll_resultbarscale = 1; // scale of result bar (in multiples of 100 pixels)

$poll_tableheader="<table border=1 class="tb">";

$poll_rowheader="<tr>";

$poll_dataheader="<td align=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"中

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » PHP新手上路(九)-PHP教程,PHP基础
分享到: 更多 (0)

相关推荐

  • 暂无文章