script-easypoll-10

Gesponsorde koppelingen

PHP script bestanden

  1. script-easypoll-10

« Lees de omschrijving en reacties

De tabellen voor de database:

DROP TABLE IF EXISTS poll_answers;
CREATE TABLE poll_answers (
id tinyint(5) NOT NULL auto_increment,
poll_id tinyint(5) default '0',
answer varchar(100) default NULL,
votes int(4) default '0',
PRIMARY KEY (id)
) TYPE=MyISAM;

DROP TABLE IF EXISTS poll_questions;
CREATE TABLE poll_questions (
id tinyint(5) NOT NULL auto_increment,
question varchar(100) default NULL,
total_votes int(4) default '0',
PRIMARY KEY (id)
) TYPE=MyISAM;

===========================================

poll.php

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
function showpoll()
{

  // This function is to show the question, or the answer
  
  // Cookie

  $cookie = $_COOKIE['poll'];
  
  // Check if cookie exsists
  if (empty($cookie))
  {

    // Show the last question (function)
    showlast();
  }
else {
    // Check if cookie is still valid (function)
    cookiecheck($cookie);
  }
}

function
showlast()
{

  // This function shows the last question and its answers
  
  // Collect the question from the DB

  $query = mysql_query("SELECT id, question FROM poll_questions ORDER BY id DESC LIMIT 0,1");
  $num = mysql_num_rows($query);
  
  if (empty($num))
  {

    echo "Error: Didn't find questions!";
  }
else {
    $question = mysql_fetch_row($query);
    
    // Start of the form
    echo "<form name=\"poll\" action=\"pollvote.php?redirect=".$_SERVER['REQUEST_URI']."\" method=\"post\">\n";
    
    // Show the question
    echo $question[1]."<br>\n";

    // Collect the answers from the DB
    $query = mysql_query("SELECT id, answer FROM poll_answers WHERE poll_id = '$question[0]' ORDER BY id");
    $num = mysql_num_rows($query);
    if (empty($num))
    {

        echo "Error: Didn't find any answers!";
    }
else {
      while ($answer = mysql_fetch_row($query))
      {

        // Show the answers
        echo "<input name=\"answer\" type=\"radio\" onclick=\"document.poll.submit()\" value=\"".$answer[0]."\" id=\"poll".$answer[0]."\">\n";
        echo "<a href=\"javascript:document.poll.submit()\" onmouseover=\"document.poll.poll".$answer[0].".checked=true\">".$answer[1]."</a><br>\n";
      }
    }

    
    // Close the form
    echo "</form>";
  }
}

function
cookiecheck($cookie)
{

  // This function checks if the poll_id in the cookie is the same as the poll_id from the last question
  // if so, it will show the answers, else it will show the last question

  // Collect the last poll_id FROM the DB

  $query = mysql_query("SELECT id FROM poll_questions ORDER BY id DESC LIMIT 0,1");
  $num = mysql_num_rows($query);

  if (empty($num))
  {

    echo "Error: Didn't find last poll!";
  }
else {
    // Check if the last poll_id is equal to the poll_id from the cokkie
    $last = mysql_fetch_row($query);

    if ($last[0] == $cookie)
    {

      // Show the results (function)
      showresults($cookie);
    }
else {
      // Show the question (function)
      showlast();
    }
  }
}

function
showresults($cookie)
{

  // This function will show the results of the poll, it's seen after voting

  // Collect the question from the DB

  $query = mysql_query("SELECT id, question, total_votes FROM poll_questions WHERE id = '$cookie'");
  $num = mysql_num_rows($query);

  if (empty($num))
  {

    echo "Error: Didn't find last poll!";
  }
else {
    $question = mysql_fetch_row($query);
    echo $question[1]."<br>\n";
    
    // Antwoorden ophalen
    $query = mysql_query("SELECT id, answer, votes FROM poll_answers WHERE poll_id = '$question[0]' ORDER BY id");
    $num = mysql_num_rows($query);

    if (empty($num))
    {

        echo "Error: Didn't find answers!";
    }
else {
      // Calculate percentage of 1 vote
      $vote = 100 / $question[2];
      
      // Show the answers with a image and a percentage
      while ($answer = mysql_fetch_row($query))
      {

        // Laat de antwoorden zien
        echo $answer[1] . "<br>\n";
        // Uitrekenen hoeveel procent deze heeft
        $percent = round($vote * $answer[2]);
        echo "<img src=\"poll.gif\" height=\"10\" width=\"".$percent."\" alt=\"".$percent."%\" border=\"0\">\n";
        echo $percent . "%<br>\n";
      }

      echo "Total: ".$question[2]." votes\n";
    }
  }
}


mysql_connect("","","");
mysql_select_db("");
showpoll();
?>


===========================================

pollvote.php
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
<?php
// Connect to the database
mysql_connect("","","");
mysql_select_db("");

// Variables
$answer = $_POST['answer'];
$redirect = $_GET['redirect'];

// Check if $answer isn't empty
if (empty($answer))
{

  header("Location: ".$redirect);
}
else {
  // Update poll_answers
  $query = mysql_query("UPDATE poll_answers SET votes = votes +1 WHERE id = '$answer'");
  if (!$query)
  {

    echo "Error: Can't save vote!";
  }
else {
    // Collect the poll_id from the DB
    $query = mysql_query("SELECT poll_id FROM poll_answers WHERE id = '$answer'");
    $poll_id = mysql_fetch_row($query);

    // Update total_votes
    $query = mysql_query("UPDATE poll_questions SET total_votes = total_votes + 1 WHERE id = '$poll_id[0]'");
    if (!$query)
    {

      echo "Error: Can't save vote!";
    }
else {
      // Set the cookie
      setcookie("poll", $poll_id[0], time()+3600*24*90);
      
      // Send back to $redirect
      header("Location: ".$redirect);
    }
  }
}

?>


===========================================

polladmin.php (NIET BESCHIKBAAR IN HET VOORBEELD!)
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
<?php

// Connect to the database
mysql_connect("","","");
mysql_select_db("");

// Variables
$action = addslashes($_GET['action']);
if(is_numeric($_GET['step']))$step = $_GET['step'];
if (empty($step)) $step = 1;

if ($action == "add")
{

  if ($step == "1")
  {

    // Here we will make a form. The first <input> is for the question
    ?>

    <form action="polladmin.php?action=add&step=2" method="post">
    Question: <input type="text" name="question" size="50"><br>
    <?php
    // I'm lazy. I don't feel like making like 10 input, so I'll use a for()-loop
    for($i=1;$i<11;$i++)
    {

      echo "Answer ".$i.": <input type=\"text\" name=\"answer".$i."\" size=\"50\"><br>\n";
    }

    
    // End of the form, with a button
    ?>

    <input type="submit" value="Make poll!">
    </form>
    <?php
  } else if ($step == "2") {
    // In this step we will put the question and the answers in the DB

    // First, add the question to the DB

    $question = addslashes($_POST['question']);
    $query = mysql_query("INSERT INTO poll_questions (question) VALUES ('$question')");

    if (!$query)
    {

      echo "Error: Can't write question to the DB!";
      exit;
    }


    // Collect the poll_id of the poll we just made
    $query = mysql_query("SELECT LAST_INSERT_ID() FROM poll_questions");
    if (!$query)
    {

      echo "Error: Can't collect poll_id!";
      exit;
    }

    $poll_id = mysql_fetch_row($query);

    // Again a for()-loopm, now to check answers
    for($i=1;$i<11;$i++)
    {

      // Spoof posted values
      $answer = "answer".$i;
      $answer = $_POST[$answer];

      // If $_POST[$antwoord] is empty, don't do anything
      if (!empty($answer))
      {

        // Save the answer
        $query = mysql_query("INSERT INTO poll_answers (poll_id, answer) VALUES ('$poll_id[0]','$answer')");
        if (!$query)
        {

          echo "Error: Can't write answer to the DB!";
          exit;
        }
      }
    }

    
    echo "Your poll has been created!<br>";
  }
}

?>

<a href="polladmin.php?action=add">Add</a>

 
 

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.