8. 投票系統(tǒng)

  在許多時(shí)候,我們需要收集上網(wǎng)者和網(wǎng)友們的意見(jiàn)。例如:新版頁(yè)面與舊版頁(yè)面的比較;對(duì)某一事情的看法;對(duì)體育比賽結(jié)果的預(yù)測(cè)等等。這時(shí)候,你需要一個(gè)非常有效的網(wǎng)上調(diào)查系統(tǒng) " /> aiai久久,中文在线免费,亚洲国产日韩欧美

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

PHP新手上路(九)

建設(shè)一個(gè)簡(jiǎn)單交互的網(wǎng)站(五)

8. 投票系統(tǒng)

  在許多時(shí)候,我們需要收集上網(wǎng)者和網(wǎng)友們的意見(jiàn)。例如:新版頁(yè)面與舊版頁(yè)面的比較;對(duì)某一事情的看法;對(duì)體育比賽結(jié)果的預(yù)測(cè)等等。這時(shí)候,你需要一個(gè)非常有效的網(wǎng)上調(diào)查系統(tǒng)。使用php就可以非常方便地實(shí)現(xiàn)你的這一構(gòu)想。

8.1 投票系統(tǒng)(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>新版頁(yè)面調(diào)查</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">與舊版頁(yè)面相比較您覺(jué)得新版頁(yè)面:</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">網(wǎng)頁(yè)更精美</span> <br>
<input type="radio" name="poll_voteNr" value="3" >
<span class="pt9">沒(méi)什么改進(jìn)</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">查看調(diào)查結(jié)果</A>
</form>
<?
/*
如果想增加其它的選項(xiàng)可直接加上即可
*/
}else{
$descArray=array(1=>"信息量更大",
2=>"網(wǎng)頁(yè)更精美",
3=>"沒(méi)什么改進(jìn)",
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編歷數(shù)組
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 "總共投票次數(shù):<font color=red> $poll_sum</font>";
echo $poll_tableFooter;
echo "<br>";
echo "<input type="submit" name="Submit1" value="返回主頁(yè)" 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>

注釋?zhuān)簭纳厦嫖覀兛梢钥闯鲈撏镀毕到y(tǒng)的基本過(guò)程:
1、打開(kāi)文件取得數(shù)據(jù)到數(shù)組$curArray(文件不存在則初始化數(shù)組$curArray)
2、編歷數(shù)組,處理數(shù)據(jù)得到所需值
3、計(jì)算百分比,控制統(tǒng)計(jì)bar圖像寬度
4、將數(shù)據(jù)保存到"data.pol"中

   這里有一點(diǎn)是需要注意:這里的data.pol文本文件需要有寫(xiě)權(quán)限。   

php技術(shù)PHP新手上路(九),轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 安新县| 奉新县| 崇明县| 册亨县| 洛隆县| 出国| 宣恩县| 抚宁县| 东丰县| 察雅县| 蒙自县| 翼城县| 南雄市| 枣庄市| 海城市| 固镇县| 五家渠市| 武隆县| 汤原县| 洛浦县| 营口市| 沁阳市| 涪陵区| 恩平市| 沂水县| 阿荣旗| 彩票| 怀安县| 株洲市| 长岛县| 阿克陶县| 天全县| 通化市| 涟源市| 建平县| 兴国县| 阳朔县| 廉江市| 恩平市| 昌乐县| 中卫市|