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.
01 | create database poll;<br /> |
03 | create table poll_results (<br /> |
04 | candidate varchar(30),<br /> |
07 | insert into poll_results values<br /> |
10 | ( 'Kurang Bagus' , 0)<br /> |
12 | <p>grant all privileges<br /> |
14 | to poll@localhost<br /> |
Setelah kita membuat Database(db)nya,selajutnya kita siapkan form untuk Pengisian data vottingnya.
vote.html
01 | &lt;html&gt;<br /> |
02 | &lt;head&gt;<br /> |
03 | &lt;title&gt;Polling&lt;/title&gt;<br /> |
04 | &lt;head&gt;<br /> |
05 | &lt;body&gt;<br /> |
06 | &lt;h1&gt;Polling&lt;/h1&gt;<br /> |
07 | &lt;p&gt;Bagaimana Menurut Kamu Tampilan Blog Ini?&lt;/p&gt;<br /> |
08 | &lt;form method=post action="show_poll.php"&gt;<br /> |
09 | &lt;input type=radio name=vote value="bagus"&gt;Bagus&lt;br /&gt;<br /> |
10 | &lt;input type=radio name=vote value="Lumayan"&gt;Lumayan&lt;br /&gt;<br /> |
11 | &lt;input type=radio name=vote value="Kurang Bagus"&gt;Kurang Bagus&lt;br /&gt;&lt;br /&gt;<br /> |
12 | &lt;input type=submit value="Show results"&gt;<br /> |
13 | &lt;/form&gt;<br /> |
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.php005 | <p> $vote = $_REQUEST [ 'vote' ];</p> |
007 | <p> if (! $db_conn = mysql_connect( 'localhost' , 'poll' , 'poll' ))<br /> |
009 | echo 'Could not connect to db&lt;br /&gt;' ;<br /> |
012 | @mysql_select_db( 'poll' );</p> |
013 | <p> if (! empty ( $vote ))<br /> |
015 | $vote = addslashes ( $vote );<br /> |
016 | $query = "update poll_results<br /> |
017 | set num_votes = num_votes + 1<br /> |
018 | where candidate = '$vote' ";<br /> |
019 | if (!( $result = @mysql_query( $query , $db_conn )))<br /> |
021 | echo 'Tidak Bisa Konek Ke Database&lt;br /&gt;' ;<br /> |
025 | <p> $query = 'select * from poll_results' ;<br /> |
026 | if (!( $result = @mysql_query( $query , $db_conn )))<br /> |
028 | echo 'Tidak Bisa Konek Ke Database&lt;br /&gt;' ;<br /> |
031 | $num_candidates = mysql_num_rows( $result ); </p> |
032 | <p> $total_votes =0;<br /> |
033 | while ( $row = mysql_fetch_object ( $result ))<br /> |
035 | $total_votes +=Â $row -&gt;num_votes;<br /> |
037 | mysql_data_seek( $result , 0);</p> |
041 | <p>putenv( 'GDFONTPATH=C:\WINDOWS\Fonts' );<br /> |
042 | $width =500;Â Â Â Â Â Â Â |
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 /> |
050 | $small_size = 12;<br /> |
051 | $text_indent = 10; </p> |
052 | <p> $x = $left_margin + 60;<br /> |
054 | $bar_unit = ( $width -( $x + $right_margin )) / 100;Â Â </p> |
055 | <p> $height = $num_candidates * ( $bar_height + $bar_spacing ) + 50;Â Â </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 /> |
079 | ImageTTFText( $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> |
085 | <p> while ( $row = mysql_fetch_object ( $result ))<br /> |
087 | if ( $total_votes &gt; 0)<br /> |
088 | $percent = intval ( round (( $row -&gt;num_votes/ $total_votes )*100));<br /> |
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 &gt; 0)<br /> |
096 | $right_value = intval ( round (( $row -&gt;num_votes/ $total_votes )*100));<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 , " $row -&gt;candidate"); Â </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 -&gt;num_votes. '/' . $total_votes );</p> |
107 | <p> $y = $y +( $bar_height + $bar_spacing );<br /> |
112 | Header( 'Content-type:Â image/png' );<br /> |
113 | ImagePNG( $im );Â Â </p> |
117 | ImageDestroy( $im );<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