Novice on the road please take care of PHP Novice Road (ix)

Source: Internet
Author: User
Building a simple interactive website (v)
8. Voting system
In many cases, we need to collect the views of Internet users and netizens. For example: a comparison between a new page and a previous page, a view of something, a prediction of the outcome of a sporting event, etc. At this point, you need a very effective online survey system. You can easily implement this idea with PHP.
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 =24h
}
?>


<title>New Page Survey</title>




Compared to the previous version of the page, you think the new page:

if (!isset ($submit)) {
?>

/*
If you want to add additional options, you can add them directly.
*/
}else{
$descArray =array (1=> "Greater amount of information",
2=> "More Beautiful Pages",
3=> "No Improvement",
4=> "Other"
);
$poll _resultbarheight = 9; Height in pixels of percentage bar in result table
$poll _resultbarscale = 1; Scale of result bar (in multiples of pixels)
$poll _tableheader= "



"; $poll _rowheader= " "; $poll _dataheader= " "; $poll _rowfooter= " "; $poll _tablefooter= "
";
$poll _datafooter= "
";
$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 series Group
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 = * $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: From the above we can see the basic process of the voting system:
1. Open file to get data to array $curarray (file does not exist initialize array $curarray)
2, compiling the group, processing data to get the required value
3, calculate the percentage, control statistics bar image width
4. Save the data to "Data.pol"
Here's one thing to note: The Data.pol text file here requires write access.

The above introduces the novice on the road please take care of PHP novice on the Road (ix), including the Novice on the road please take care of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.