views:

69

answers:

3

I'm working on a contest where users vote for contestants. Each contestant will have a bio. I was wondering what would be the best way to approach this? Should I do this in php or javascript? Should I use a database to collect data? Should I use sqlite3? If I use sqlite3 how do i install that on my mac? I'm very new to all this but I'm a quick learner. Thanks for any advice.

+1  A: 

You will have to use PHP for this if you want people from all over to be able to vote. JavaScript is client sided [ok fine, there's SSJS, but that isn't common]...

Code is simple

<?php
mysql_connect(addr, usr, pass) or die(mysql_error());
mysql_select_db(dbName) or die(mysql_error());

//To vote for user0
mysql_query("UPDATE tableName SET votes=votes+1 WHERE candidateId=0");
?>

... of course you'd have to have a method of determining the same user viewing your site from a different user [No, session isn't appropriate. Cookies can be circumvented as well]

Format for table "tableName":
***************************************************
* candidateId - int - autoincrement * votes - int *
***************************************************
* 0                                 * 0           *
* 1                                 * 0           *
***************************************************
ItzWarty
wow okay thanks. So do I have to put the db on the server or what? Can you point be to some good reading on this? I really have no clue what i'm doing.
creocare
To use this code as written, the server will need a mysql database with a table named `tablename` and columns named `votes` and `candidateId`. Also, you'll need to change the `0` in the where clause to be dependent on a query string or something. Note that this code does nothing at all to prevent users from voting multiple times, e.g. by refreshing the page. The simple, somewhat reliable way to fix this is to have a second table of ip addresses, and insert voters into it, disallowing votes if user is already there.
Brian
Ok so I got this to work. The only problem is it fires off everytime I open the page this code is on. I tried using onclick but that didn't seem to work. is there a handler in php for this?
creocare
A: 

It sounds like you're new to creating dynamic websites. I would suggest googling how to set yourself up a basic LAMP setup (Linux or your operating system of choice, an apache web server, MySql, and PHP). There are installers (like XAMPP for instance) that will install all the stuff you need.

Once you get that started you can start diving into some tutorials. If you're looking for one close to your goal project, this one looks promising: http://ad1987.blogspot.com/2009/02/reddit-style-voting-with-php-mysql-and.html

You can just add a bio field to the table they're using in that tutorial and boom, done.

Good Luck!

ddombrow
+1  A: 

If you wanted to take all the fun out of it, you could try to tailor a service like http://www.surveymonkey.com/ to fit your solution. Of course, this being a programmer's web site...

LesterDove