½¨ÉeÒ»¸o¼oµ¥½»»¥µÄÍøÕ¾(Îa)
8. ͶƱϵͳ
¡¡¡¡ÔÚÐi¶aʱºo£¬ÎÒÃÇÐeÒªÊÕ¼¯ÉÏÍøÕߺÍÍøÓÑÃǵÄÒa¼u¡£ÀýÈ磺аaeÒ³ÃaeÓe¾É°aeÒ³ÃaeµÄ±È½Ï£»¶ÔijһÊÂÇeµÄ¿´¨£»¶ÔÌaÓý±ÈÈu½a¹uµÄÔ¤²aµÈµÈ¡£Õaʱºo£¬ÄaÐeÒªÒ»¸odz£ÓÐЧµÄÍøÉϵ÷²eϵͳ¡£Ê¹ÓÃPHP¾Í¿ÉÒÔdz£½±aµØʵÏÖÄaµÄÕaÒ»¹¹Ïe¡£
8.1 ͶƱϵͳ£¨mypolls.php3£(C)£º
<?
$status=0;
if(isset($polled)&&($polled=="c-e")){
$status=1;
}
if(isset($poll)&&($status==0)){
setcookie("polled","c-e",time()+86400,"/");#time=24h
}
?>
"; $poll_dataFooter=" | "; $poll_rowFooter="
// 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±aÀuÊý×e
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);
}
}
?>
×¢ÊÍ£º´ÓÉÏÃaeÎÒÃÇ¿ÉÒÔ¿´³o¸ÃͶƱϵͳµÄ»u±¾¹ý³Ì£º
1¡¢´o¿ªÎļþÈ¡µÃÊý¾Ýµ½Êý×e$curArray£¨Îļþ²»´aeÔÚÔo³oʼ»¯Êý×e$curArray£(C)
2¡¢±aÀuÊý×e£¬´¦ÀiÊý¾ÝµÃµ½ËuÐeÖµ
3¡¢¼ÆËa°Ù*ֱȣ¬¿ØÖÆͳ¼ÆbarͼÏñ¿i¶È
4¡¢½«Êý¾Ý±£´aeµ½"data.pol"ÖÐ
¡¡¡¡ ÕaÀiÓÐÒ»µaÊÇÐeҪעÒa£ºÕaÀiµÄdata.polÎı¾ÎļþÐeÒªÓÐдȨÏÞ¡£