Jump to content
xisto Community
DSGamer3002

How Would I Go About Making A Simple "counting" Script?

Recommended Posts

I plan on making a script for basic voting between different options, and I'd like to know what PHP coding I would require. Basically, each choice will be as simple as this:

<form method="post" action="process.php">Best falsetto?<br><br><input type="radio" name="1"> Person A<br><input type="radio" name="2"> Person B<br><input type="submit" value="Submit"></form>

What PHP would be used to basically add 1 value to a specified .txt file based on which option is chosen? (Like, if person A was selected, it would add 1 to persona.txt, and if person B was chosen, it would add 1 to personb.txt)

Thanks in advance to whoever helps. I'm not good at this kind of intermediate/advanced PHP. :P

Share this post


Link to post
Share on other sites

The script you write will need to check the value of $_POST[name] and increase the counter for that name. Easy enough I suppose, but consider that I could sit at my computer all day and vote a number of times. This will skew the results, so I would do some checking of IP's and allow only one vote per IP. Another method to control the 'duplication' problem is to set a Cookie or a Session value to control duplicate votes. Or a time-based solution, allowing votes only after a period of time.

There are likely several voting or Poll scripts available around the 'net all ready to go. Check at Hotscripts.com or Google on it. No sense re-inventing the wheel.

Or search around the Xisto. looks like someone else had the same question once: http://forums.xisto.com/topic/35066-any-idea-where-i-can-find-a-good-voting-poll-script-multiple-topics/

Another possible solution is to start a Forum. Most of them include some sort of Voting or Poll system. Check out AEF Forums at http://anelectron.com/

Share this post


Link to post
Share on other sites

By the way, input elements (except a few) require the value attribute to be set in order to be able to send a value to the server-side script, otherwise the variable containing the POST information will be empty.

Share this post


Link to post
Share on other sites

I think you might be better to use a database instead of a file to store the data, there is a little more PHP to be learned (or used, depending on your level) but its well worth it.

the difference in difficulty isnt really that much and the advantages are pretty big...As Jlhaslip said you might want to add a method for stopping a user voting more than once. an IP check is a good idea but an IP changes a lot so cookies might be an idea, or if the user has logged in then you can of course use their username, thats a reliable method.

With a DB you could do several things much easier than a file methinks. You could add details to it easily like their IP/name then which option they voted for. Then use code to count how many votes were for each option, this is quite easy using something like:

EDIT: I just realized that i completely missed anything about inserting values to a database but follow the instructions at the bottom about a tutorial site and it will show you how to input data into a DB.

$result_option_1 = mysql_query("SELECT * FROM table WHERE option='1'", $link);$votes_1 = mysql_num_rows($result_option_1);echo "Total votes for option one is: $votes_1";

you would repeat code like this for each option. Its rather basic and the code above is messy and wouldnt work (its not complete with a connection to an actual database, this is just the query part of it, and i left out a few backslashes to make it look better 8-)) but the general idea is there, query the database to find all votes for option one, then count the number of rows (which is the number of votes for option one) and then tell the user that number.

If you need help with the code search google for "php mysql tutorial tizag.com" and you should find a nice, easy to follow tutorial string about php and mysql and yo will learn what i just wrote above and how to fully complete such a code.

I wrote this assuming it will help you, if you already know this then dont be offended, i just have no idea what sort of level you're at with PHP so im writing for a beginner.
Edited by shadowx (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.