Prodi Sistem Informasi | Belajar HTML dan PHP | Skripsi SI
Pesantren Katabah
1000 Penghafal Quran
Pengobatan Ruqyah Mandiri
Sistem Informasi (S1)
Manajemen Informatika
Blog | Kontak | Siap Kerja | Sertifikat | PrivacyPolicy | Inggris Arab | Daftar Isi

Wednesday, January 1, 2014

Cara Membuat Polling Dengan PHP



1.      Software:
·         WampServer 2.2
·         Apache 2.2.21
·         PHP 5.3.10
·         MySQL 5.5.20
2.      Buat database: ikhbaar
3.      Buat tabel: polling

Column
Type
Length
Extra
id_polling
Integer
5
Auto_Increment
pertanyaan
Varchar
100

jawab_a
Varchar
50

jawab_b
Varchar
50

jawab_c
Varchar
50

jawab_d
Varchar
50

jawab_e
Varchar
50

hasil_a
Integer
5

hasil_b
Integer
5

hasil_c
Integer
5

hasil_d
Integer
5

hasil_e
Integer
5

aktif
Enum
'Y','N'


4.      Buat gambar dengan nama: 5blue.png

5.       Buat file: 5polling.php
Ketik script berikut:
<?php
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));

mysql_connect("localhost","root","");
mysql_select_db("ikhbaar");

switch($_GET[act]){
  // Tampil Polling
 
  default:
     $polling=mysql_query("select * from polling where aktif='Y'");
     $p=mysql_fetch_array($polling);
     echo "<form method=POST action='?act=hasilpoling'>";
     echo "$p[pertanyaan]<br />";
     echo "<input type='hidden' name='id' value='$p[id_polling]' />";
     echo "<input type='radio' name='pilihan' value='a' />$p[jawab_a]<br />
                             <input type='radio' name='pilihan' value='b' />$p[jawab_b]<br />
                             <input type='radio' name='pilihan' value='c' />$p[jawab_c]<br />
                             <input type='radio' name='pilihan' value='d' />$p[jawab_d]<br />
                             <input type='radio' name='pilihan' value='e' />$p[jawab_e]<br />
                             <input type='submit' value='Vote' name='vote' />";
     echo "</form>";
     echo "<a href='?act=lihatpolling'>Lihat Hasil Polling</a>";
  break;
 
  case "hasilpoling":
     if (isset($_COOKIE["poling"])) {
        echo "Sorry, anda sudah pernah melakukan voting terhadap poling ini.";
     } else {
                 // membuat cookie dengan nama poling
                 // cookie akan secara otomatis terhapus dalam waktu 24 jam
                
                 setcookie("poling", "sudah poling", time() + 3600 * 24);
                 echo "<span class=posting>&#187; <b>Hasil Polling</b></span><br /><br />";

                 if ($_POST["pilihan"]=="a") {
                             $u=mysql_query("UPDATE polling SET hasil_a=hasil_a+1 WHERE id_polling='$_POST[id]'");
                 }
                 elseif ($_POST["pilihan"]=="b") {
                             $u=mysql_query("UPDATE polling SET hasil_b=hasil_b+1 WHERE id_polling='$_POST[id]'");
                 }
                 elseif($_POST["pilihan"]=="c") {
                             $u=mysql_query("UPDATE polling SET hasil_c=hasil_c+1 WHERE id_polling='$_POST[id]'");
                 }
                 elseif($_POST["pilihan"]=="d") {
                             $u=mysql_query("UPDATE polling SET hasil_d=hasil_d+1 WHERE id_polling='$_POST[id]'");
                 }
                 elseif($_POST["pilihan"]=="e") {
                             $u=mysql_query("UPDATE polling SET hasil_e=hasil_e+1 WHERE id_polling='$_POST[id]'");
                 }

                 echo "<p align=center>Terimakasih atas partisipasi Anda mengikuti poling kami<br /><br />
        Hasil poling saat ini: </p><br />";
     }
  //break;
 
  case "lihatpolling":
     //echo "<span class=posting>&#187; <b>Hasil Polling</b></span><br /><br />";
     echo "<table width=100% style='border: 1pt dashed #0000CC;padding: 10px;font-size:9pt; font-family:Arial;'>";

                 $jml=mysql_query("SELECT * FROM polling WHERE aktif='Y'");
                 $j=mysql_fetch_array($jml);
 
                 $jml_vote = $j[hasil_a] + $j[hasil_b] + $j[hasil_c] + $j[hasil_d] + $j[hasil_e];
 
                 $pros_a = sprintf("%2.1f",(($j[hasil_a]/$jml_vote)*100));
                 $pros_b = sprintf("%2.1f",(($j[hasil_b]/$jml_vote)*100));
                 $pros_c = sprintf("%2.1f",(($j[hasil_c]/$jml_vote)*100));
                 $pros_d = sprintf("%2.1f",(($j[hasil_d]/$jml_vote)*100));
                 $pros_e = sprintf("%2.1f",(($j[hasil_e]/$jml_vote)*100));
                
                 $gbr_va = $pros_a * 3;
                 $gbr_vb = $pros_b * 3;
                 $gbr_vc = $pros_c * 3;
                 $gbr_vd = $pros_d * 3;
                 $gbr_ve = $pros_e * 3;
                
                 echo "<tr><td width=120>$j[jawab_a] ($j[hasil_a]) </td><td>
          <img src='5blue.png' width='$gbr_va' height=18 border=0> $pros_a %
          </td></tr>
          <tr><td width=120>$j[jawab_b] ($j[hasil_b]) </td><td>
          <img src='5blue.png' width='$gbr_vb' height=18 border=0> $pros_b %
          </td></tr>
          <tr><td width=120>$j[jawab_c] ($j[hasil_c]) </td><td>
          <img src='5blue.png' width='$gbr_vc' height=18 border=0> $pros_c %
          </td></tr>
          <tr><td width=120>$j[jawab_d] ($j[hasil_d]) </td><td>
          <img src='5blue.png' width='$gbr_vd' height=18 border=0> $pros_d %
          </td></tr>
          <tr><td width=120>$j[jawab_e] ($j[hasil_e]) </td><td>
          <img src='5blue.png' width='$gbr_ve' height=18 border=0> $pros_e %
          </td></tr>";
                 echo "</table>
        <p>Jumlah Voting: <b>$jml_vote</b></p>
        <p><a href='javascript:history.back()'>&laquo; back</a></p>";
  break;
}

/*Sumber:
1. Edi Kurniawan. Membuat Poling dengan php. http://edibukit.blogspot.com/2011/10/membuat-poling-dengan-php.html
2. Bayu Widayat. http://masbay.wordpress.com */

?>



6.      Hasilnya:




7.      Klik tulisan Lihat Hasil Polling, akan muncul gambar:





***

Sumber:

1.    Edi Kurniawan. Membuat Poling dengan php. edibukit.blogspot.com/2011/10/membuat-poling-dengan-php.html

2. Bayu Widayat. masbay.wordpress.com
"Boleh Konsultasi Masuk Jurusan Sistem Informasi via IG atau Tiktok."
Tips Skripsi Program Studi Sistem Informasi

No comments:

Post a Comment