Building a simple interaction website (v) 8. in many cases, we need to collect opinions from netusers and netizens. For example, comparison between the new page and the old page, views on a specific event, and predictions on the results of a sports competition. At this time, you need a very effective online survey system. With PHP, you can easily implement this idea. Build a simple interaction website(V.)
8.Voting system
In many cases, we need to collect opinions from Internet users and netizens. For example, comparison between the new page and the old page, views on a specific event, and predictions on the results of a sports competition. At this time, you need a very effective online survey system. UsePHPYou can easily implement this idea.
8.1Voting system (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 = 24 h
}
?>
</SPAN> <SPAN style = "FONT-SIZE: 9pt"> New page survey <SPAN lang = EN-US>
Bgcolor= "# FFFFFF">
Compared with the old page, you think the new page is:
< Br>
If (! Isset($ Submit )){
?>
/*
If you want to add other options, add them directly.
*/
} Else {
$DescArray= Array (1 =>"More information ",
2 =>"Better Web Page ",
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_rowHeaderTr
Poll_dataHeader
Poll_rowFooter
Poll_tableFooter
";$= "<> ";$="
"; $Poll_dataFooter=" | ";$=" Tr> ";$="
";
$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 optionsEditingCalendar 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 "<Br> ";
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);
}
}
?>
Note: We can see the basic process of the voting system from the above:
1, Open the file to get data to the array$CurArray(If the file does not exist, the array is initialized.$CurArray)
2, Editing calendar array, processing data to get the required value
3, Calculating percentage, controlling statisticsBarImage width
4Save data"Data. pol"Medium
Note the following:Data. polWrite permission is required for text files.