simpele-poll

Gesponsorde koppelingen

PHP script bestanden

  1. simpele-poll

« Lees de omschrijving en reacties

#--> SQL
CREATE TABLE `poll` (
`vraag1` int(10) unsigned NOT NULL default '0',
`vraag2` int(10) unsigned NOT NULL default '0',
`vraag3` int(10) unsigned NOT NULL default '0',
`vraag4` int(10) unsigned NOT NULL default '0',
`vraag5` int(10) unsigned NOT NULL default '0',
`vraag6` int(10) unsigned NOT NULL default '0'
) TYPE=MyISAM;

INSERT INTO `poll` VALUES (0, 0, 0, 0, 0, 0);



Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#-->    index.php
<?php
session_start();

#-->    connecting to database
@mysql_connect("localhost", "mysql_user", "mysql_pass") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

#-->    cookie domein
$cookie_domain = "www.joudomein.nl";

#-->    poll vraag
$poll_vraag = "Onder welke scherm resolutie draai jij Windows";

#-->    poll options
$poll[1] = '640 x 480';
$poll[2] = '800 x 600';
$poll[3] = '1024 x 768';
$poll[4] = '1152 x 864';
$poll[5] = '1280 x 1024';
$poll[6] = 'Hoger';

#-->    hoeveel pollvragen hebben we
$num = count($poll);

#-->    kijken of er al eerder gestemd is
if(!isset($_SESSION['pollvoted']) && !isset($_COOKIE['pollres']))
{

    #-->    kijken of er een optie gekozen is
    if(isset($_POST['poll']))
    {

        #-->    updaten van de gekozen optie in de poll
        $sql = "UPDATE `poll` SET `vraag".$_POST['poll']."` = (vraag".$_POST['poll']." +1)";
        $res = @mysql_query($sql) or die(mysql_error());
        #-->    kijken of de database ook werkelijk geupdate is
        if(mysql_affected_rows())
        {

            $content = '<br><br><br><center><b>Updating Poll</b></center> <META HTTP-EQUIV=Refresh CONTENT="1; URL=index.php">';
            #-->    voor als cookies niet aanstaan op de client
            $_SESSION['pollvoted'] = 'oke';
            #-->    setting cookie
            setcookie('pollres', 'voted', time()+17558400, '/', $cookie_domain);
        }

        else
        {
            $content = '<br><br><br><center><b>Het updaten van de database ging niet helemaal goed</b></center>';
        }
    }

    else
    {    
        #-->    begin building form and table
        $content = "<form style=\"margin:0px;\" action=\"index.php\" method=\"post\">\n&nbsp;<img src=\"images/poll_icon.gif\"> <b>Poll</b>\n<br>\n<table width=\"540\" cellpadding=\"1\" cellspacing=\"0\" border=\"1\" bordercolor=\"#ffffff\"><tr bgcolor=\"#003D89\"><td colspan=\"3\">&nbsp;<b style=\"color:fff;\">".$poll_vraag."</b></td></tr>\n";
        #-->    building table met poll options
        for($i=1; $i<=$num; $i++)
        {

            $content .= '<tr bgcolor="'.($i % 2 ? '#F4F4F4' : '#E8E8E8').'"><td><input type="radio" name="poll" id="'.$i.'" value="'.$i.'"> <label for="'.$i.'"><b> '.$poll[$i].'</b></label></td</tr>'."\n";            
        }

        #-->    end building form and table
        $content .= "</table>\n&nbsp;<input type=\"submit\" name=\"submit\" value=\"Vote\">\n</form>\n";
    }
}

else
{
    #-->    het aantal antwoorden per poll vraag uit de db trekken
    $sql = "SELECT * FROM `poll`";
    $res = @mysql_query($sql) or die(mysql_error());
    $data = mysql_fetch_assoc($res);

    #-->    totaal aantal stemmen bepalen
    $total_votes = 0;
    for($i=1; $i<=$num; $i++)
    {

        $total_votes += $data['vraag'.$i];
    }


    $content = "&nbsp;<img src=\"images/poll_icon.gif\"> <b>Poll</b>\n<br>\n<table width=\"540\" cellpadding=\"3\" cellspacing=\"0\" border=\"1\" bordercolor=\"#ffffff\">\n<tr bgcolor=\"#003D89\"><td colspan=\"4\"><b style=\"color:fff;\">".$poll_vraag."</b></td></tr>\n";
    for($i=1; $i<=$num; $i++)
    {

        #-->    procent per poll optie uitrekenen
        $proc = round(($data['vraag'.$i] / $total_votes) * 100, 1);
        if(!strstr($proc, '.')) $proc .= '.0';
        #-->    building html table
        $content .= '<tr bgcolor="'.($i % 2 ? '#F4F4F4' : '#E8E8E8').'"><td width="140">&nbsp;<b>'.$poll[$i]. '</b></td><td width="220">&nbsp;<img src="images/poll_left.gif" width="2" height="10" alt=""><img src="images/poll.gif" height="10" width="'.round(($proc * 2)).'" alt=""><img src="images/poll_right.gif" width="2" height="10" alt=""> </td><td align="right">&nbsp; '.$proc.'% &nbsp;</td><td align="right" nowrap>&nbsp;('.$data['vraag'.$i].' votes)&nbsp;</td></tr>'."\n";
    }

    $content .= "</table>\n<br>\n&nbsp;Totaal is er <b>".$total_votes."</b> keer gestemd";
}


?>

<html>
<head>
<title><?php echo $poll_vraag; ?></title>
<style type="text/css">
body {    font-family:Verdana, Arial, Helvetica, sans-serif;
        font-size:12px;
        text-align:center;}
td {    font-family:Verdana, Arial, Helvetica, sans-serif;
        font-size:12px;}
</style>
</head>
<body>
<br>
<br>
<br>
<div style="text-align:left; padding:40px; width:600px; margin:0px auto;">
<?php
    echo $content;
?>

<br>
<br>
<br>
<br>
<br>
<center><span style="color:#CCCCCC">created 11-01-2005 by nano</span></center>
</div>
</body>
</html>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.