Tutorial PHP "Membuat Vooting atau Polling

|| || || 2 komentar
Kali ini kita  akan blajar mengenai cara pembuatan voting pada sebuah website,yang tentu nya saya buat secara sederhana agar dapat dimodifikasi lagi ,supaya lebih menarik tentunya…Tutorial ini saya baca dari sebuah buku dan sepertinya menarik juga untuk pembelajaran

1.Pertama kita siapkan sebuah DATABASE dengan 3buah table di dalam nya seperti berikut:
 -> Database: `cepot-web`


-> CREATE TABLE `voting` (
  `kriteria` varchar(50) NOT NULL,
  `value` int(10) unsigned NOT NULL,
  `kriteriaid` smallint(6) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`kriteriaid`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;


3.Ketik script php berikut pada NOTEPAD atau media script editor lainya,Disini saya menggunakan “KOMODO EDIT 5”.  Script php nya sbb:
<!doctype html publick "-//W3C//DTD HTML 4.0 //EN">
<html>
    <head>
        <title>Tutor VOTING</title>
    </head>
    <body>
<?php
$server ='localhost';
$user ='root';
$password='';
$database = 'cepot-web';
$koneksi=mysql_connect($server,$user,$password)
or die ("DATA BASE TIDAK TERHUBUNG...!");
mysql_select_db($database,$koneksi);

?>
<table border="6" style="border-collapse: collapse"
bordercolor="blue" width="100% height="56">
    <tr>
        <td align="center" width="100%" height="26"> <b><font color="green" face="verdana"><h2>Apakkah saya tampan???</h2></font></b></td>
    </tr>
    <form action="voting.php" method="post">
        <tr>
            <td width="100%" height="26">
                <table border="0" cellpadding="0" cellspacing="0"
                style="border-collapse: collapse" bordercolor="#78602c"
                width="100%">
                    <tr>
                        <td width="100%" bgcolor="yellow">
                            <?php
                            $qr=mysql_query("SELECT kriteria,kriteriaid
                                            FROM voting order by kriteria", $koneksi)
                            or die ("kriteria voting Salah !");
                            while ($row = mysql_fetch_array($qr))
                            {
                                echo "<input type=radio name=pilihan value=$row[kriteriaid]
                                > &nbsp;&nbsp;&nbsp;<u>$row[kriteria]</u><br>";
                            }
                            ?>
                            &nbsp;</td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td width="100%" height="26"> <input type=submit name=submit value=vote></td>
        </tr>
    </form>
    <tr>
        <td align="center" width="100%" height="61"> <b><font color="green" face="verdana">
        <marquee bgcolor="black"><h4>Hasil Pooling Anda - Hasil Pooling Anda - Hasil Pooling Anda - Hasil Pooling Anda </h4></font></b></marquee>
        <hr noshade color="#000000" size="1"></td>
    </tr>
        <tr>
            <td width="100%" height="50">
                <table border="0" cellpadding="0" cellspacing="0"
                style="border-collapse: collapse" width="100%"
                height="50" bordercolor="blue">
                    <?php
                   
                   $qr=mysql_query("UPDATE voting Set value = value + 1
                                    WHERE kriteriaid='$_POST[pilihan]'",$koneksi)
                    or die ("Kriteria VOOTING Salah !");
                   
                    $qr=mysql_query("SELECT Sum(value) as TotalPersen FROM voting",$koneksi)
                    or die ("Query Tidak Tersambung Atau Gagal Ada kesalahan..!!");
                    $row = mysql_fetch_array($qr);
                    $Total=$row['TotalPersen'];
                   
                    $qr= mysql_query("SELECT Max(value) as NilaiMax
                                    FROM voting",$koneksi) or die
                    ("Query MAX SALAH Tidak Tersambung...!!");
                    $row =mysql_fetch_array($qr);
                    $pengali = 100/$row['NilaiMax'];
                   
                    $qr = mysql_query("SELECT kriteria, value FROM
                                      voting order by kriteria",$koneksi)
                    or die ("query GAGAL TERSAMBUNG...!");
                 
                    while ($row = mysql_fetch_array($qr))
                    {
                        $persen=round($row['value']* $pengali,0);
                        $ValPersen=round($row['value']/$Total*100,2);
                        echo "<tr> <td width=25% height=22>$row[kriteria]
                       </td><td width=75% height=18>";
                       echo "<table border=1 cellpadding=0 cellspacing=0 style='border-collapse: collapse'
                       width=$persen% height=18>";
                       echo "<tr> <td width=100% bgcolor=yellow align=center>$ValPersen%</td>";
                       echo "</tr></table></td></tr>";
                    }
                               
                    ?>
                    </table>
                </td>
            </tr>
        </body>
    </html>

4.Setelah DATABASE dan script PHP selesai di buat ,simpan scipt PHP dengan
Nama “voting.php” sebab nama tersebut terkait dengan script “$_POST” di atas.
Dan buka pada web browser anda hasil nya sebagai berikut :

 Selesai .

SQL - creating a database

|| || || 1 komentar

Creating a database inside of SQL Express has its advantages. After launching Microsoft's SQL Server Management Studio Express application, simply right-clicking on the Databases folder of the Object Explorer gives you the option to create a New Database. After selecting the New Database... option, name your database "MyDatabase" and press "OK".
Create New Database Command

Name the New SQL Database Window

Now is the time to press the New Query button located toward the top of the screen, just above the Object Explorer pane.
New Query Button
Pressing this button offers an empty tab. All SQL query statements (code) that we will be exploring will be entered here and executed against the SQL Express database.
If you haven't yet created a new database, you may also create a database by typing the following SQL query statement into your new empty query tab, and then pressing the Execute button or striking the (F5) key.

SQL Create Database Query:

CREATE DATABASE MyDatabase;
After executing this query, SQL will notify you that your query has run successfully and that the database was created successfully. If you receive an error message instead, Google the error message for troubleshooting advice. (Vista users must verify that they are running SQL Server Management Studio Express with administrator privileges.)
Congratulations! You have executed your first SQL command and written what is perhaps your first bit of SQL code.