Home >Backend Development >PHP Tutorial >PHP newbies on the road (9)_PHP tutorial

PHP newbies on the road (9)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:00:53962browse

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

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)){
?>


More information


The web page is more beautiful


No improvement

< input type="radio" name="poll_voteNr" value="4" >
Others



View survey results

/*
If you want to add other options, you can add them directly Just go up
*/
}else{
$descArray=array(1=>"More information",
2=>"The webpage is more beautiful",
3 =>"No improvement",
4=>"Others"
);
$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_rowFooter="";
$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 calendar array
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 "Total number of votes: $poll_sum";
echo $poll_tableFooter;
echo "
";
echo "< input type="submit" name="Submit1" value="Return to homepage" onClick="javascript:location='http://gophp.heha.net/index.html'">";
echo " < ;input type="submit" name="Submit2" value="Revote" 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);
}
}
?>



Note: From the above we can see the basic process of the voting system:
1. Open the file to obtain the data into the array $curArray (if the file does not exist, initialize the array $curArray)
2. Calendar array , process the data to obtain the required value
3. Calculate the percentage, control the statistical bar image width
4. Save the data to "data.pol"

There is one thing to note here: The data.pol text file requires write permission. 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316996.htmlTechArticleBuilding a simple interactive website (5) 8. Voting system In many cases, we need to collect Internet users and netizens their opinions. For example: Comparison between the new version of the page and the old version of the page; for a certain thing...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
";
$poll_dataFooter="