PHP newbie (9) building a simple interactive website (5)
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. With PHP, you can easily implement this idea.
8.1 voting 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
}
?>
New page survey
Compared with the old page, you think the new page is:
If (! Isset ($ submit )){
?>
/*
If you want to add other options, add them directly.
*/
} Else {
$ DescArray = array (1 => "more information ",
2 => "more beautiful webpages ",
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_dataFooter =" | ";$ Poll_rowFooter ="
";$ Poll_tableFooter ="
";
$ 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 calendars 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 votes: $ 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 );
}
}
?>
Note: We can see the basic process of the voting system from the above:
1. open the file and get the data to the array $ curArray (if the file does not exist, initialize the array $ curArray)
2. Compile the calendar array and process the data to obtain the required value.
3. calculate the percentage and control the bar image width.
4. save the data to "data. pol"
Note that the data. pol text file must have the write permission.