Page 1 of 1

creating a very basic/simple voting poll

Posted: Sat Aug 07, 2004 9:44 pm
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

Posted: Sat Aug 07, 2004 11:25 pm
by tim
google.com

hotscripts.com

evilwalrus.com

Posted: Sun Aug 08, 2004 2:51 am
by phpcoder
y u want to invent wheel again :roll:
got to
http://www.proxy2.de
u will get good poll script there

Posted: Sun Aug 08, 2004 6:47 am
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 ?

Posted: Sun Aug 08, 2004 7:43 am
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

Posted: Sun Aug 08, 2004 7:58 am
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>

Posted: Sun Aug 08, 2004 10:33 am
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....

Posted: Sun Aug 08, 2004 11:35 am
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

:!:

Posted: Sun Aug 08, 2004 12:32 pm
by buddok
feyd | don't go there.

Posted: Sun Aug 08, 2004 3:00 pm
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

Posted: Mon Aug 09, 2004 7:30 am
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

:!:

Posted: Mon Aug 09, 2004 7:47 am
by buddok
:!: :!: :!: I HAVE A SCRIPT NOW :!: :!: :!:

Posted: Mon Aug 09, 2004 8:09 am
by tim
congrats

feyd | ....