利用php打印出九行菱形,PHP新手上路(九)

建设一个简单交互的网站(五)

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

}

?>

新版页面调查
与旧版页面相比较您觉得新版页面:

if(!isset($submit)){

?>

信息量更大

网页更精美

没什么改进

其它

查看调查结果

/*

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

*/

}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="

$poll_rowHeader="

";

$poll_dataHeader="

";

$poll_dataFooter="

";

$poll_rowFooter="

";

$poll_tableFooter="

";

$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 "总共投票次数: $poll_sum";

echo $poll_tableFooter;

echo "
";

echo "";

echo " ";

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);

}

}

?>

注释:从上面我们可以看出该投票系统的基本过程:

1、打开文件取得数据到数组$curArray(文件不存在则初始化数组$curArray)

2、编历数组,处理数据得到所需值

3、计算百分比,控制统计bar图像宽度

4、将数据保存到"data.pol"中

这里有一点是需要注意:这里的data.pol文本文件需要有写权限。

时间: 2006-10-07

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值