creating a very basic/simple voting poll

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
buddok
Forum Commoner
Posts: 50
Joined: Tue May 25, 2004 2:44 pm

creating a very basic/simple voting poll

Post by buddok »

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
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

google.com

hotscripts.com

evilwalrus.com
User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

Post by phpcoder »

y u want to invent wheel again :roll:
got to
http://www.proxy2.de
u will get good poll script there
User avatar
buddok
Forum Commoner
Posts: 50
Joined: Tue May 25, 2004 2:44 pm

Post by buddok »

hey thanks for those sites but im lookin for more of a simple lil tutorial please ? and wat was the invent whell thing about :P ?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

'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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

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:

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)
);
On a page we want to display the last question, and it's possible answers:

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);
time to generate some html

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>
And offcourse we need to handle the choosen answers

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());
}
And at last we want to display the results for each answer:

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);
And generate output

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>
User avatar
buddok
Forum Commoner
Posts: 50
Joined: Tue May 25, 2004 2:44 pm

Post by buddok »

thats realy great thanks but i didnt get some im a real newbie im so sorry :( and i need radio buttons with three answers if anyone wants to help, o an could you tell me when there is a change in page also :)

thanks....
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

in my opinion, we've been generous enough

nooone is going to code this thing for you. Least not in exchange for some goods/services.

You have the links, you have the internet, you have google.

Do the research yourself or pay someone. Cant expect everything to be handed to you

:!:
User avatar
buddok
Forum Commoner
Posts: 50
Joined: Tue May 25, 2004 2:44 pm

Post by buddok »

feyd | don't go there.
User avatar
buddok
Forum Commoner
Posts: 50
Joined: Tue May 25, 2004 2:44 pm

Post by buddok »

does anyone no a tutorial site that will help me learn the most basic poll with no images using flat file or MySQL and no admin
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

tim wrote:in my opinion, we've been generous enough

nooone is going to code this thing for you. Least not in exchange for some goods/services.

You have the links, you have the internet, you have google.

Do the research yourself or pay someone. Cant expect everything to be handed to you

:!:
User avatar
buddok
Forum Commoner
Posts: 50
Joined: Tue May 25, 2004 2:44 pm

Post by buddok »

:!: :!: :!: I HAVE A SCRIPT NOW :!: :!: :!:
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

congrats

feyd | ....
Post Reply