Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> How Would I Go About Making A Simple "counting" Script?
DSGamer3002
post Feb 17 2008, 11:12 AM
Post #1


Newbie [Level 1]
*

Group: Members
Posts: 24
Joined: 5-February 08
From: Business, Nonya
Member No.: 57,376



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:

CODE
<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. ph34r.gif
Go to the top of the page
 
+Quote Post
jlhaslip
post Feb 17 2008, 11:23 AM
Post #2


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,874
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



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 Trap17. looks like someone else had the same question once: http://www.trap17.com/forums/index.php?s=&...st&p=239559

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
Go to the top of the page
 
+Quote Post
truefusion
post Feb 17 2008, 04:54 PM
Post #3


Ephesians 6:10-17
Group Icon

Group: [MODERATOR]
Posts: 1,861
Joined: 22-June 05
From: The World of Gentoo
Member No.: 8,528
T17 GFX Crew



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.
Go to the top of the page
 
+Quote Post
shadowx
post Feb 17 2008, 09:04 PM
Post #4


A clever man learns from his own mistakes, a WISE man learns from those of OTHERS
*********

Group: [HOSTED]
Posts: 884
Joined: 12-April 06
From: Essex, UK
Member No.: 21,719



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.

CODE
$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.

This post has been edited by shadowx: Feb 17 2008, 09:07 PM
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Script: Php Jukebox(4)
  2. Parse: Error Unexpected T_lnumber(4)
  3. Transfer Variables To Another Php Script(8)
  4. Watermark Your Image With Simple Php Script(34)
  5. Script That Tracks The User Status(4)
  6. Wappychat_oldskool(15)
  7. Php Search Engine Script For Mysql Database(11)
  8. Free Auction Script(6)
  9. Wappy Buddy V1.10 - Tibia Gold Edition By Wappy & Jon Roig(3)
  10. What Kind Of Script Do You Need ?(15)
  11. Creatting A Playlist Through Php(5)
  12. Is This A Good Script?(9)
  13. Html Code Tester. Online Script(15)
  14. Making Sure They Did Not Leave Any Required Fields Blank(3)
  15. Php Sessions And Post Variables Issues(1)
  1. Will This Code Work(5)
  2. Counting Variables?(4)
  3. Php Downloads Script(3)
  4. Script Help Required: Undefined Variable(3)
  5. Script Not Working(6)
  6. Library Script(6)
  7. Forum Script(3)
  8. Php Rediret Script(12)
  9. Download Script For Mp3 Files(0)
  10. How Do I Connect To Live Database With Php Script?(6)
  11. Need Help Installing Dolphin Community Script!(5)
  12. Guessing Php Script(0)
  13. How To Make A View New Post Script?(5)


 



- Lo-Fi Version Time is now: 24th July 2008 - 06:34 AM