Membuat Modul Polling Dengan PHP

Biasanya dengan mudah kita mendapatkan source2 widget polling dari om goOgle.Yang kali ini ingin saya paparkan adalah bagaimana caranya kalau kita ingin ” Membuat module Polling sendiri ? “.Dengan Php tentunya.Sebenarnya menggunakan Ajax juga bisa,tapi kita coba pelajari saja dulu yah dengan Php Script yang menggunakan database Sql server.




Langkah pertama kita siapkan dulu DataBase untuk php nya.

01create database poll;<br />
02use poll;<br />
03create table poll_results (<br />
04 candidate varchar(30),<br />
05 num_votes int<br />
06);<br />
07insert into poll_results values<br />
08 ('Bagus', 0),<br />
09 ('Lumayan', 0),<br />
10 ('Kurang Bagus', 0)<br />
11; </p>
12<p>grant all privileges<br />
13on poll.*<br />
14to poll@localhost<br />
15identified by 'poll';


Setelah kita membuat Database(db)nya,selajutnya kita siapkan form untuk Pengisian data vottingnya.

vote.html

01&amp;lt;html&amp;gt;<br />
02&amp;lt;head&amp;gt;<br />
03 &amp;lt;title&amp;gt;Polling&amp;lt;/title&amp;gt;<br />
04&amp;lt;head&amp;gt;<br />
05&amp;lt;body&amp;gt;<br />
06&amp;lt;h1&amp;gt;Polling&amp;lt;/h1&amp;gt;<br />
07&amp;lt;p&amp;gt;Bagaimana Menurut Kamu Tampilan Blog Ini?&amp;lt;/p&amp;gt;<br />
08&amp;lt;form method=post action=&quot;show_poll.php&quot;&amp;gt;<br />
09&amp;lt;input type=radio name=vote value=&quot;bagus&quot;&amp;gt;Bagus&amp;lt;br /&amp;gt;<br />
10&amp;lt;input type=radio name=vote value=&quot;Lumayan&quot;&amp;gt;Lumayan&amp;lt;br /&amp;gt;<br />
11&amp;lt;input type=radio name=vote value=&quot;Kurang Bagus&quot;&amp;gt;Kurang Bagus&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;<br />
12&amp;lt;input type=submit value=&quot;Show results&quot;&amp;gt;<br />
13&amp;lt;/form&amp;gt;<br />
14&amp;lt;/body&amp;gt;


ok..form pengisian votting sudah kita buat,selanjutnya apa bro ? selanjutnya kita akan buat Php Untuk Menampilkan Data Votting Yang Sudah Berhasil di hasilkan oleh Sql Server.

Save As show_poll.php

001&lt;?php<br />
002/*******************************************<br />
003Query Database untuk mendapatkan info polling<br />
004*******************************************/</p>
005<p>$vote=$_REQUEST['vote'];</p>
006<p>// membuat koneksi ke database </p>
007<p>if (!$db_conn = mysql_connect('localhost', 'poll', 'poll'))<br />
008{<br />
009 echo 'Could not connect to db&amp;lt;br /&amp;gt;';<br />
010 exit;<br />
011};<br />
012@mysql_select_db('poll');</p>
013<p>if (!empty($vote))<br />
014{<br />
015 $vote = addslashes($vote);<br />
016 $query = &quot;update poll_results<br />
017 set num_votes = num_votes + 1<br />
018 where candidate = '$vote'&quot;;<br />
019 if(!($result = @mysql_query($query, $db_conn)))<br />
020 {<br />
021 echo 'Tidak Bisa Konek Ke Database&amp;lt;br /&amp;gt;';<br />
022 exit;<br />
023 }<br />
024};</p>
025<p>$query = 'select * from poll_results';<br />
026if(!($result = @mysql_query($query, $db_conn)))<br />
027{<br />
028 echo 'Tidak Bisa Konek Ke Database&amp;lt;br /&amp;gt;';<br />
029 exit;<br />
030}<br />
031$num_candidates = mysql_num_rows($result); </p>
032<p>$total_votes=0;<br />
033while ($row = mysql_fetch_object ($result))<br />
034{<br />
035 $total_votes +=  $row-&amp;gt;num_votes;<br />
036}<br />
037mysql_data_seek($result, 0);</p>
038<p>/*******************************************<br />
039 Kalkulasi Membuat Grapic Votting<br />
040*******************************************/</p>
041<p>putenv('GDFONTPATH=C:\WINDOWS\Fonts');<br />
042$width=500;        // mengatur lebar hasil image votting<br />
043$left_margin = 50;<br />
044$right_margin= 50;<br />
045$bar_height = 40;<br />
046$bar_spacing = $bar_height/2;<br />
047$font = 'arial';<br />
048$title_size= 16;<br />
049$main_size= 12;<br />
050$small_size= 12;<br />
051$text_indent = 10; </p>
052<p>$x = $left_margin + 60;<br />
053$y = 50;<br />
054$bar_unit = ($width-($x+$right_margin)) / 100;   </p>
055<p>$height = $num_candidates * ($bar_height + $bar_spacing) + 50;   </p>
056<p>/*******************************************<br />
057 Membuat Image hasil Votting<br />
058*******************************************/</p>
059<p>$im = ImageCreateTrueColor($width,$height);   </p>
060<p>$white=ImageColorAllocate($im,255,255,255);<br />
061$blue=ImageColorAllocate($im,0,64,128);<br />
062$black=ImageColorAllocate($im,0,0,0);<br />
063$pink = ImageColorAllocate($im,255,78,243); </p>
064<p>$text_color = $black;<br />
065$percent_color = $black;<br />
066$bg_color = $white;<br />
067$line_color = $black;<br />
068$bar_color = $blue;<br />
069$number_color = $pink;</p>
070<p>ImageFilledRectangle($im,0,0,$width,$height,$bg_color);   </p>
071<p>ImageRectangle($im,0,0,$width-1,$height-1,$line_color);   </p>
072<p>$title = 'Poll Results';<br />
073$title_dimensions = ImageTTFBBox($title_size, 0, $font, $title);<br />
074$title_length = $title_dimensions[2] - $title_dimensions[0];<br />
075$title_height = abs($title_dimensions[7] - $title_dimensions[1]);<br />
076$title_above_line = abs($title_dimensions[7]);<br />
077$title_x = ($width-$title_length)/2;<br />
078$title_y = ($y - $title_height)/2 + $title_above_line;<br />
079ImageTTFText($im, $title_size, 0, $title_x, $title_y,<br />
080 $text_color, $font, $title); Â </p>
081<p>ImageLine($im, $x, $y-5, $x, $height-15, $line_color);   </p>
082<p>/*******************************************<br />
083 Draw data ke graphic<br />
084*******************************************/</p>
085<p>while ($row = mysql_fetch_object ($result))<br />
086{<br />
087 if ($total_votes &amp;gt; 0)<br />
088 $percent = intval(round(($row-&amp;gt;num_votes/$total_votes)*100));<br />
089 else<br />
090 $percent = 0;</p>
091<p> $percent_dimensions = ImageTTFBBox($main_size, 0, $font, $percent.'%');<br />
092 $percent_length = $percent_dimensions[2] - $percent_dimensions[0];<br />
093 ImageTTFText($im, $main_size, 0, $width-$percent_length-$text_indent,<br />
094 $y+($bar_height/2), $percent_color, $font, $percent.'%'); Â </p>
095<p> if ($total_votes &amp;gt; 0)<br />
096 $right_value = intval(round(($row-&amp;gt;num_votes/$total_votes)*100));<br />
097 else<br />
098 $right_value = 0;</p>
099<p> $bar_length = $x + ($right_value * $bar_unit);   </p>
100<p> ImageFilledRectangle($im, $x, $y-2, $bar_length, $y+$bar_height, $bar_color);   </p>
101<p> ImageTTFText($im, $main_size, 0, $text_indent, $y+($bar_height/2),<br />
102 $text_color, $font, &quot;$row-&amp;gt;candidate&quot;); Â </p>
103<p> ImageRectangle($im, $bar_length+1, $y-2,<br />
104 ($x+(100*$bar_unit)), $y+$bar_height, $line_color);   </p>
105<p> ImageTTFText($im, $small_size, 0, $x+(100*$bar_unit)-50, $y+($bar_height/2),<br />
106 $number_color, $font, $row-&amp;gt;num_votes.'/'.$total_votes);</p>
107<p> $y=$y+($bar_height+$bar_spacing);<br />
108}   </p>
109<p>/*******************************************<br />
110 Image Option<br />
111*******************************************/<br />
112Header('Content-type:  image/png');<br />
113ImagePNG($im);   </p>
114<p>/*******************************************<br />
115 Hasil Akhir<br />
116*******************************************/<br />
117ImageDestroy($im);<br />
118?&gt;<br />


Nanti Hasil Untuk Votting Php Nya kira2 seperti gambar ini :

Sudah Selesai Kita membuat module Votting/polling dengan PHp,Mudah Bukan Membuatnya? jangan lupa juga untuk menyesuaikan parametter databasenya. :)

Posting Komentar

Lebih baru Lebih lama