下载本文示例代码
b>建设一个简单交互的网站(五)
8. 投票系统
在许多时候,我们需要收集上网者和网友们的意见。例如:新版页面与旧版页面的比较;对某一事情的看法;对体育比赛结果的预测等等。这时候,你需要一个非常有效的网上调查系统。使用PHP就可以非常方便地实现你的这一构想。
8.1 投票系统(mypolls.php3):
$#@60;?
$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
}
?$#@62;
$#@60;html$#@62;
$#@60;head$#@62;
$#@60;title$#@62;新版页面调查$#@60;/title$#@62;
$#@60;meta http-equiv="Content-Type" content="text/html;
charset=gb2312"$#@62;
$#@60;style type="text/css"$#@62;
$#@60;!--
.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
}
--$#@62;
$#@60;/style$#@62;
$#@60;/head$#@62;
$#@60;body bgcolor="#FFFFFF"$#@62;
$#@60;div class="head"$#@62;与旧版页面相比较您觉得新版页面:$#@60;/div$#@62;$#@60;br$#@62;
$#@60;?
if(!isset($submit)){
?$#@62;
$#@60;form action="myPolls.php3" method="get"$#@62;
$#@60;input type="radio" name="poll_voteNr" value="1"
checked $#@62;
$#@60;span class="pt9"$#@62;信息量更大$#@60;/span$#@62; $#@60;br$#@62;
$#@60;input type="radio" name="poll_voteNr" value="2"
$#@62;
$#@60;span class="pt9"$#@62;网页更精美$#@60;/span$#@62; $#@60;br$#@62;
$#@60;input type="radio" name="poll_voteNr" ue="3"
$#@62;
$#@60;span class="pt9"$#@62;没什么改进$#@60;/span$#@62; $#@60;br$#@62;
$#@60;input type="radio" name="poll_voteNr" value="4"
$#@62;
$#@60;span class="pt9"$#@62;其它$#@60;/span$#@62; $#@60;br$#@62;
$#@60;input type="submit" name="submit" value="OK"$#@62;
$#@60;input type="hidden" name="poll" value="vote"$#@62;
$#@60;A HREF="myPolls.php3?submit=OK" class="p9"$#@62;查看调查结果$#@60;/A$#@62;
$#@60;/form$#@62;
$#@60;?
/*
如果想增加其它的选项可直接加上即可
*/
}else{
$descArray=array(1=$#@62;"信息量更大",
2=$#@62;"网页更精美",
3=$#@62;"没什么改进",
4=$#@62;"其它"
);
$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="$#@60;table border=1 class=\"tb\"$#@62;";
$poll_rowHeader="$#@60;tr$#@62;";
$poll_dataHeader="$#@60;td align=center$#@62;";
$poll_dataFooter="$#@60;/td$#@62;";
$poll_rowFooter="$#@60;/tr$#@62;";
$poll_tableFooter="$#@60;/table$#@62;";
$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$#@60;=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 $#@62; 0)
{
$poll_percentScale = (int)($poll_percent * $poll_resultBarScale);
}
printf(" %.2f %% (%d)", $poll_percent, $poll_optionCount);
echo $poll_dataFooter;
}
echo $poll_rowFooter;
}
echo "总共投票次数:$#@60;font color=red$#@62; $poll_sum$#@60;/font$#@62;";
echo $poll_tableFooter;
echo "$#@60;br$#@62;";
echo "$#@60;input type=\"submit\" name=\"Submit1\"
value=\"返回主页\" onClick=\"javascript:location=\"$#@62;";
echo " $#@60;input type=\"submit\" name=\"Submit2\"
value=\"重新投票\" onClick=\"javascript:location=\"$#@62;";
if(isset($poll)){
// write counter file
$fp = fopen($coutfile, "wt");
reset($curArray);
while (list($Key, $Value) = each($curArray))
{
$tmp = sprintf( "%s %d\n", $Key, $Value);
fwrite($fp, $tmp);
}
// close file
fclose($fp);
}
}
?$#@62;
$#@60;/body$#@62;
$#@60;/html$#@62;
注释:从上面我们可以看出该投票系统的基本过程:
1、打开文件取得数据到数组$curArray(文件不存在则初始化数组$curArray)
2、编历数组,处理数据得到所需值
3、计算百分比,控制统计bar图像宽度
4、将数据保存到"data.pol"中
这里有一点是需要注意:这里的data.pol文本文件需要有写权限。
b>建设一个简单交互的网站(五)
8. 投票系统
在许多时候,我们需要收集上网者和网友们的意见。例如:新版页面与旧版页面的比较;对某一事情的看法;对体育比赛结果的预测等等。这时候,你需要一个非常有效的网上调查系统。使用PHP就可以非常方便地实现你的这一构想。
8.1 投票系统(mypolls.php3):
$#@60;?
$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
}
?$#@62;
$#@60;html$#@62;
$#@60;head$#@62;
$#@60;title$#@62;新版页面调查$#@60;/title$#@62;
$#@60;meta http-equiv="Content-Type" content="text/html;
charset=gb2312"$#@62;
$#@60;style type="text/css"$#@62;
$#@60;!--
.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
}
--$#@62;
$#@60;/style$#@62;
$#@60;/head$#@62;
$#@60;body bgcolor="#FFFFFF"$#@62;
$#@60;div class="head"$#@62;与旧版页面相比较您觉得新版页面:$#@60;/div$#@62;$#@60;br$#@62;
$#@60;?
if(!isset($submit)){
?$#@62;
$#@60;form action="myPolls.php3" method="get"$#@62;
$#@60;input type="radio" name="poll_voteNr" value="1"
checked $#@62;
$#@60;span class="pt9"$#@62;信息量更大$#@60;/span$#@62; $#@60;br$#@62;
$#@60;input type="radio" name="poll_voteNr" value="2"
$#@62;
$#@60;span class="pt9"$#@62;网页更精美$#@60;/span$#@62; $#@60;br$#@62;
$#@60;input type="radio" name="poll_voteNr" ue="3"
$#@62;
$#@60;span class="pt9"$#@62;没什么改进$#@60;/span$#@62; $#@60;br$#@62;
$#@60;input type="radio" name="poll_voteNr" value="4"
$#@62;
$#@60;span class="pt9"$#@62;其它$#@60;/span$#@62; $#@60;br$#@62;
$#@60;input type="submit" name="submit" value="OK"$#@62;
$#@60;input type="hidden" name="poll" value="vote"$#@62;
$#@60;A HREF="myPolls.php3?submit=OK" class="p9"$#@62;查看调查结果$#@60;/A$#@62;
$#@60;/form$#@62;
$#@60;?
/*
如果想增加其它的选项可直接加上即可
*/
}else{
$descArray=array(1=$#@62;"信息量更大",
2=$#@62;"网页更精美",
3=$#@62;"没什么改进",
4=$#@62;"其它"
);
$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="$#@60;table border=1 class=\"tb\"$#@62;";
$poll_rowHeader="$#@60;tr$#@62;";
$poll_dataHeader="$#@60;td align=center$#@62;";
$poll_dataFooter="$#@60;/td$#@62;";
$poll_rowFooter="$#@60;/tr$#@62;";
$poll_tableFooter="$#@60;/table$#@62;";
$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$#@60;=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 $#@62; 0)
{
$poll_percentScale = (int)($poll_percent * $poll_resultBarScale);
}
printf(" %.2f %% (%d)", $poll_percent, $poll_optionCount);
echo $poll_dataFooter;
}
echo $poll_rowFooter;
}
echo "总共投票次数:$#@60;font color=red$#@62; $poll_sum$#@60;/font$#@62;";
echo $poll_tableFooter;
echo "$#@60;br$#@62;";
echo "$#@60;input type=\"submit\" name=\"Submit1\"
value=\"返回主页\" onClick=\"javascript:location=\"$#@62;";
echo " $#@60;input type=\"submit\" name=\"Submit2\"
value=\"重新投票\" onClick=\"javascript:location=\"$#@62;";
if(isset($poll)){
// write counter file
$fp = fopen($coutfile, "wt");
reset($curArray);
while (list($Key, $Value) = each($curArray))
{
$tmp = sprintf( "%s %d\n", $Key, $Value);
fwrite($fp, $tmp);
}
// close file
fclose($fp);
}
}
?$#@62;
$#@60;/body$#@62;
$#@60;/html$#@62;
注释:从上面我们可以看出该投票系统的基本过程:
1、打开文件取得数据到数组$curArray(文件不存在则初始化数组$curArray)
2、编历数组,处理数据得到所需值
3、计算百分比,控制统计bar图像宽度
4、将数据保存到"data.pol"中
这里有一点是需要注意:这里的data.pol文本文件需要有写权限。
下载本文示例代码
PHP新手上路(九)PHP新手上路(九)PHP新手上路(九)PHP新手上路(九)PHP新手上路(九)PHP新手上路(九)PHP新手上路(九)PHP新手上路(九)PHP新手上路(九)PHP新手上路(九)PHP新手上路(九)PHP新手上路(九)PHP新手上路(九)PHP新手上路(九)PHP新手上路(九)
阅读(102) | 评论(0) | 转发(0) |