Help - Search - Members - Calendar
Full Version: PHP/mySQL poll
InsanelyMac Forum > Everything Else > Creativity > Web and Graphical Design
nickg331
I'm making a PHP/mySQL poll for a site, and I was just wondering if anyone had any suggestions/help/ideas. Here's what I have so far:

In the mySQL database, I have three tables: poll, poll_options, and poll_votes.

Poll

This table has three columns: poll_id (an autoincrementing number), poll_ques (varchar, the actual question), and poll_ans (a number that tells us how many possible answers we have for that poll).

Poll_options

This table also has three columns: poll_associd (an autoincrementing number that corresponds to poll_id in the poll table), poll_ansnum (the number of that particular answer), and poll_choice (varchar, the actual text of the answer)

Poll_votes

This table once again has three columns: poll_ip (I want to record the IP addresses of those who vote in this table to prevent them from voting again), poll_voted (the id number of which poll they voted on), and poll_select (the number of the answer they chose).

So, this is how it would work:

If I had the following poll:

Who is coolest?
Option 1: Me
Option 2: You
Option 3: Tyler Durden

This is what it would look like in the database:

Table poll
poll_id---------poll_ques----------------poll_ans
1---------------Who is coolest?----------3

Table poll_options
poll_associd--------poll_ansnum--------poll_choice
1---------------------1---------------------Me
1---------------------2---------------------You
1---------------------3---------------------Tyler Durden

Table poll_votes
poll_ip----------------poll_voted------------poll_select
000.000.000.004-----1-----------------------3
000.000.000.003-----1-----------------------2
000.000.000.001-----1-----------------------3
000.000.000.002-----1-----------------------1

(sorry about the dashes - it wouldn't let me put spaces)

Now I understand the concepts of everything, but I'm kind of new with PHP, so any suggestions on coding would be greatly appreciated.

Thanks
(MoC)
I really don't go beyond the basics with this lang so I can't help!
ozboi
The easiest way would be first to start by writing out the steps you would need to take in psuedo-code an work from there. This is what it might look like after the "vote" button is pressed on a poll:

1. Check form values
1.1. If no option selected end script and report error
2. Connect to database
3. Retrieve IP of user (user_ip)
4. Check user_ip to responses already collected
4.1. Retrieve responses from database for current poll (SQL = SELECT * FROM Poll_votes WHERE poll_select = $_POST['poll_id'])
4.2. If Rows > 0 end script and report that user has already voted
5. Add IP and response to Poll_votes (SQL = INSERT INTO Poll_votes (poll_ip, poll_voted, poll_select) VALUES ($user_ip], $_POST['poll_id'], $_POST['poll_value']))
6. Thank user and display results
nickg331
thanks a lot- i'm trying my hand at it, but if anyone could give any more specific code that would be great.

thanks
ozboi
For specific code you can try browsing the PHP section of HotScripts.com ... they have an entire section just on polling.
nickg331
i have but nothing quite suits my needs...
inimicus
Do you want someone to write the code for you?

ozboi pretty much summed (if that's not a word, it is now) it up pretty well.

If you don't know how to put the whole thing together, just piece every step ozboi laid out together. You can certainly learn better by trying it out yourself.

If you get stuck, we can help you with your code... but I know I certainly don't have the time to write parts out for you from scratch.

You seem to be on the right track. Keep us posted.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.