Jump to content

PHP Poll

- - - - -

  • Please log in to reply
4 replies to this topic

#1
brade8000

brade8000

    Newbie

  • Members
  • PipPip
  • 21 posts
Hi,
I have my own website and I would like to have a poll and a results page I am happy
to use a database and would also like to customize it with CSS any codes would be
appreciated.
Thanks.

#2
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
  • Location:/etc/passwd
Are you wanting help making one??
I guess the simple way would be to set up a table:
Votes
ID (int, primary, AI, etc)
option (int)
then have a vote page where a user selects a option, an entry is added to the "Votes" table:
1 | 3
In this case they chose option 3.
On the results page you just grab all the results for each option (separately) then use mysql_num_rows to work out the amount of entries with a vote for each option before displaying the data. To get a percentage, well that's math and I actually cannot remember how to do that operation...

If you want to make a bar graph of something that could be harder... I'll give you a clue it rhymes with "boogle".
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#3
brade8000

brade8000

    Newbie

  • Members
  • PipPip
  • 21 posts
Thanks,
How would I count the number of results to show on the results page and
is it possible to have it all on the one page say on my home page you vote
and then the page doesn't change only the poll does and it shows the results.
Thanks.

#4
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
  • Location:/etc/passwd

Quote

On the results page you just grab all the results for each option (separately) then use mysql_num_rows to work out the amount of entries with a vote for each option before displaying the data. To get a percentage, well that's math and I actually cannot remember how to do that operation...
Isn't that what you want?

I guess to make the poll change once you've voted you would create a PHP page that just echos out the results (above) with any styling then use Javascript to submit a vote using AJAX and then to set the DIV (for the sake of this example) to the results of the poll (fetched using AJAX).

BTW AJAX isn't a language or anything just a method for loading content on a page after its loaded.
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#5
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
As for counting the results for each poll question ( and without having to do a query for each ) I'd do it like this:

TABLE "polls":
id | question

TABLE "answers":
id | answer

TABLE "votes":
id | poll_id | answer_id ( | user | ip )

Then I'd get the results for the certain poll like this:


<?php


$poll_id = 1; //the id of the poll (for ex. the first one)

$get_votes = mysql_query("SELECT * FROM votes WHERE poll_id = '$poll_id' "); //select all votes to this poll


$votes = array(); //the array that will be containing all votes ( we add them as we retrieve them in a while loop )


while($vote = mysql_fetch_assoc($get_votes)) { //retrieve them

  $votes[$vote['answer_id']] = (int)$votes[$vote['answer_id']]+1; //add 1 vote for this particulair answer

}


//now loop through the answers and get their corresponding amount of votes from $votes[id_of_answer]


$get_answers = mysql_query("SELECT * FROM answers WHERE poll_id = '$poll_id' ");


while($answer = mysql_fetch_assoc($get_answers)) { //loop through all answers this poll has

  $id = $answer['id']; //the id of the answer -> the amount of votes for each answer we stored in $votes[id_of_answer] so for this id it would be $votes[$id]

  echo "<p>{$answer} (".(int)$votes[$id]." votes)</p>";

}


?>


Done.

But I'm not sure if this is what you want ^^. Just thought I'd share my thoughts.

Cheers.

btw, I did not test my code as I wrote it while I thought it up and I just woke up :-P




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users