Example Learning PHP voting program (ii)

Source: Internet
Author: User
Tags array config count flock include php file require

Well, the basic things have been learned, let us carry out the actual combat it! Let's start with this example program. Then you can try it on your own platform first. Believe this will get a little perceptual knowledge.

In this application a total of three file vote.php,config.php,1.gif and a file to save data (the name of the file can be set freely, where we set to Sum.txt), where vote.php is the main program file, config.php is used to set some often need to modify of information.

config.php file

<?

Title variable sets the title tag in this HTML file, which appears in the browser title bar
$title = type of reader questionnaire;

Set up the survey content. Here we use an array, and note that the "" number is used to enclose the content.
$option = Array ("Student", "worker", "peasant", "intellectual", "capitalist", "rogue");

Set the results of the survey to which file to save
$countfile = "Sum.txt";

Set the time when the same machine can vote again
$limitdate = time () +60*60*24*365;

?>

vote.php file

/* First put the configuration information, here I briefly explain the difference between require and include, require is usually placed in the front of the PHP program, PHP programs in the implementation, will first read into the Require designated file, make it a part of the PHP program page. A commonly used function can also be introduced into a Web page in this way. Include is typically placed in the processing section of the Process Control. The PHP Program page reads the included file when it is read. This way you can simplify the process of executing your program. */

<? Require "config.php";? >

/* The following section is a cookie, its expire time is set in the above config.php, and you may have found that there are two if comparison statements, the first is to detect whether there is a cookie set, The second is to prevent people from sending cookies without a voting procedure. */
<?
if (Isset ($vote) && $vote = = "Ready") $status = 1;
else $status = 0;
if ($ready = = "true")
{
Setcookie ("Vote", "Ready", $limitdate);
}
?>

< html>
< head>
< title><? echo $title?></title>
< meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

< body bgcolor= "#FFFFFF" >

Read out the original ballot results and assign them to the variable $result
<?
if (file_exists ($countfile)) {
$result = file ($countfile);
}
?>

/* The following is the main program with switch to do three branches, Case 1, view the results, Case 2, submit the choice; case 3, generate the form * *
<?
Switch ($elect) {


/* View the results, the actual is the above with the file () function read out the variables shown.
Case "View Results":
echo "< table border= ' 0 ' >";
For ($i =0 $i < count ($result); $i + +) {
echo "< tr>". < td> ". $option [$i]." </td> ";
echo "< td>". < img src= ' 1.gif ' width= ' ";
echo "$result [$i]*10";
echo "' height= ' 8 ' > </td>";
echo "< td>". "". $result [$i]. " </td></tr> ";
}
echo "</table>";
Break


/* The selection results are written to the file, note to use flock to lock the file! Although this question is not particularly prominent in the voting procedure.
Case "Submit":
if ($status = = 1) {
echo "You've already voted."
}elseif ($sugest = = 0) {
echo "You Havn ' t choice";
}else{
if ($fp =fopen ($countfile, "w")) {
if (Flock ($FP, 3)) {
For ($i =0 $i < count ($option); $i + +) {
if ($sugest = = $i + 1) {
$result [$i]= $result [$i]+1;
}else $result [$i]= $result [$i] + 0;
echo $option [$i]. "". $result [$i]. " < br> ";
Fwrite ($fp, $result [$i]. " ");
}
}else "Vote failed";
}else echo "Vote failed";
Fclose ($FP);
}
Break

/* Display the voting form * * *
Default
echo "< form action= ' vote.php ' method= ' post ' name= ' Form1 ' >";
$sum = count ($option);
for ($i =0; $i < $sum; $i + +) {
echo "< input type= ' radio ' name= ' sugest ' value=";
echo $i +1;
if ($i ==0) echo "CHECKED";
echo ">". $option [$i]. " < br> ";
}
echo "< input type= ' hidden ' name= ' ready ' value= ' true ' > ';
echo "< br>";
echo "< input type= ' submit ' name = ' elect ' value= ' submitted ' > ';
echo "< input type= ' submit ' name= ' elect ' value= ' view results ' >";
echo "</form>";
}
?>
</p>
</body>


Such a simple PHP vote application is complete, saying it is simple because the program only completes the voting application of the most important functions, there are some details of the problem not to deal with. For example, after the failure of the file write processing, the user to turn off the cookie function to cope with, and so on. The concrete consummation is left to the reader friend to solve.



Related Article

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.