Wednesday, April 11, 2012

Creating a Poll with PHP


Surely all readers familiar with the name 'poll'. polling is
polling place where visitors could vote. Given the polls, we can know what the most popular foods of Indonesia, what programming language is most preferred, etc. depending on the type of poll. Well, this time, I will teach you how to create a poll by the method of operation of the file (no database).
In this example, I create a poll about "what web programming language
The most you like. "Since this is just an example, I only include 2 options namely PHP and ASP.Anda can add your own choice of the third, fourth, and so on.

Let us begin to create polls. 4 Prepare the following files:
A. 'Asp.txt' and then type the number 0 in it.
2. 'Php.txt' and then type the number 0 in it.
Remember .. Do not forget to type the value 0 in 'asp.txt' and 'php.txt'. This is very important. There is a friend who said that why is not the way the poll program. Perhaps because he forgot to type the number 0 in it ..
3. File picture 'poll.jpg' 100x10px size. This is to make the polling percentage.
Which is also important. Create an image 'poll.jpg' with Photoshop / Corel Draw or other image processing software ..
4. 'Index.php' and then type the following script:

<title>Polling dengan operasi file created </title>

<style type="text/css">

<!--

.style1 {font-size: 24px}
.style2 {color: #0000FF}
-->
</style>
<form name="form1" method="post" action="index. php?isi=polling">
<p>
<label><span class="style1">Polling</span></label>
</p>
<p>
<label>Bahasa pemrograman web apa yang anda suka ? </label>
</p>
<p>
<label>
<input type="radio" name="vote" value="php">
PHP</label>
<br>
<label>
<input type="radio" name="vote" value="asp">
ASP</label>
</p>
<p>
<label>
<input type="submit" name="Submit" value="Submit">
</label>
<br>
</p>
</form>
<?php
if(@$_GET['isi'])
{
$vote = $_POST['vote'];
if ($vote == "") // jika belum menentukan pilihan
{
print "<font color=red>Anda belum mengisi polling</font>";
exit;
}
if($vote == "php") // menambah 1 untuk pilihan php
{
$buka = fopen("php.txt","r");
$baca = fgets($buka,65535);
fclose($buka);
$buka = fopen("php.txt","w");
$baca++;
fwrite($buka,$baca);
fclose($buka);
}
if($vote == "asp") //menambah 1 untuk pilihan asp
{
$buka = fopen("asp.txt","r");
$baca = fgets($buka,65535);
fclose($buka);
$buka = fopen("asp.txt","w");
$baca++;
fwrite($buka,$baca);
fclose($buka);
}
$buka_php = fopen("php.txt","r");
$bacaphp = fgets($buka_php,65535); //membaca nilai php yang telah di vote
fclose($buka_php);
$buka_asp = fopen("asp.txt","r");
$bacaasp = fgets($buka_asp,65535); //membaca nilai asp yang telah di vote
fclose($buka_asp);
$total_pemilih = $bacaphp+$bacaasp; //jumlah pemilih
$persentase_php = ($bacaphp/$total_pemilih)*100;
$persentase_asp = ($bacaasp/$total_pemilih*100);
print "Total pemilih : $total_pemilih <br><br>";
print "PHP : "; printf ("%1.0f","$persentase_php"); print " % <img src=poll.jpg
width=$persentase_php height=10> $bacaphp pemilih<br>";
print "ASP : "; printf ("%1.0f","$persentase_asp"); print " % <img src=poll.jpg
width=$persentase_asp height=10> $bacaasp pemilih<br>";
}
?>


No comments:

Post a Comment

SOP Door Lock Activity Reading

Door Lock Activity Reading 1        1.    Prepare DTU Unit. 2.    Switch “On” DTU Unit. 3.    Put in Staff Code DTU, press Enter. ...