creating a very basic/simple voting poll
Moderator: General Moderators
creating a very basic/simple voting poll
hey i need a realy realy realy simple script to make a poll im a newbie an need help please
i dont want any graphs or anythin jus simple pecentages please. with three options please any site or scripts very much appriciated
'invent wheel' means reapeating the process...although we would't get very far if the wheel was never invented lol.
http://www.hotscripts.com/PHP/Tips_and_ ... index.html
http://www.hotscripts.com/PHP/Tips_and_ ... index.html
Here is a little tutorial, it's untested and can probably be improved much
Let's analyse what a poll is. It is a question on which the user can answer with one of the predefined answers. Basically, you need the count how many times each answer has been choosen by a user.
Thus we create a database to store our data:
On a page we want to display the last question, and it's possible answers:
time to generate some html
And offcourse we need to handle the choosen answers
And at last we want to display the results for each answer:
And generate output
Let's analyse what a poll is. It is a question on which the user can answer with one of the predefined answers. Basically, you need the count how many times each answer has been choosen by a user.
Thus we create a database to store our data:
Code: Select all
CREATE TABLE questions(
question_id INT AUTO_INCREMENT,
question_content VARCHAR(255),
PRIMARY KEY(question_id)
);
CREATE TABLE answers(
answer_id INT,
question_id INT,
answer_content VARCHAR(255),
answer_count INT,
PRIMARY kEY(question_id, answer_id)
);Code: Select all
// connect with database
mysql_connect('host', 'user', 'pass') or die(mysql_error());
mysql_select_db('database') or die(mysql_error());
// get the latest question
$query = "SELECT * FROM questions ORDER BY question_id DESC LIMIT 1";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
mysql_free($result);
// get the available answers
$query = "SELECT * FROM answers WHERE question_id=" . $row['question_id'];
$result = mysql_query($query) or die(mysql_error());
$answers = array();
while ($row2 = mysql_fetch_assoc($result))
{
$answers[$row2['answer_id']] = $row2['answer_content'];
}
mysql_free($result);Code: Select all
<form action="" method="post">
<?= $rowї'question_content'] ?>
<input type="hidden" name="question_id" value="<?= $rowї'question_id'] ?>" />
<select name="answer_id" >
<?php foreach($answers as $key => $val) { ?>
<option value="<?= $key ?>"><?= $val ?></option>
<?php } ?>
</select>
<input type="submit" value="Submit" name="poll" />
</form>Code: Select all
if (isset($_POST['poll']))
{
// connect to the database
mysql_connect('host', 'user', 'pass') or die(mysql_error());
mysql_connect('database') or die(mysql_error());
// clean the input
$question_id = mysql_escape_string($_POST['question_id']);
$answer_id = mysql_escape_string($_POST['answer_id']);
// update the answer_count for the given question_id and answer_id
$query = 'UPDATE answers SET answer_count=answer_count+1 WHERE question_id=' . $question_id . ' AND answer_id=' . $answer_id;
$result = mysql_query($query) or die(mysql_error());
}Code: Select all
// connect with database
mysql_connect('host', 'user', 'pass') or die(mysql_error());
mysql_select_db('database') or die(mysql_error());
// clean the input
$question_id = mysql_escape_string($_GET['question_id']);
// get the latest question
$query = "SELECT * FROM questions WHERE question_id=" . $question_id;
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
mysql_free($result);
// get the sum of answer_counts for the question_id
$query = "SELECT SUM(count) AS total FROM answers WHERE question_id=" . $row['question_id'];
$result = mysql_query($query) or die(mysql_error());
$row2 = mysql_fetch_assoc($result);
mysql_free_result($result);
// now calculate for each answer the percentage
$query = "SELECT * FROM answers WHERE question_id=" . $question_id;
$result = mysql_query($query) or die(mysql_error());
$answers = array();
while ($row3 = mysql_fetch_assoc($result))
{
$answers[$row3['answer_content']] = ceil($row3['answer_count'] * 100 / row2['total']);
}
mysql_free($result);Code: Select all
<table>
<tr><th>answer_id</th><th>percentage</th></tr>
<php foreach($answers as $key => $val) { ?>
<tr><td><?= $key ?></td><td><?= $val ?></td></tr>
<?php } ?>
</table>