PHP新手上路(九)

來源:互聯網
上載者:User

建設一個簡單互動的網站(五)

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"中

   這裡有一點是需要注意:這裡的data.pol文字檔需要有寫入權限。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.