Hi..Currently creating a voting system.On which i have to save all the total votes in database.What is the format or should i say the code in counting of votes. I have already a list of candidates and radio button.I want that if i click one of the radio button for the specific candidate and submit button,it will count and save in the database..tnx in advance. Godbless

Recommended Answers

All 4 Replies

Ahh...funny enough I did something similar as one of my first large PHP projects. The code I wrote back then was quite atrocious, but its gotten better :P. It was pretty straightforward then, but if at all possible, I would use OOP. Like you said, you only need a backend now, and I would naturally suggest PHP.

Along the lines of how, you need a MySQL table with a rows for id, IP (or username if you have a user system), and vote. The vote value is the most important, since it holds the user's vote (Surprise there!). Anyway, one of the first things I tried was a MySQL table with candidates and total votes stored as a field in their table. This is a NO! There are so many problems with this system; so trust me, make the votes and the candidates separate in MySQL. I suggest a table of votes, with the vote field being an enum with the id of each candidate or the name of their corresponding radio button.

With this, all you will need to do in PHP is validate that the user hasn't already voted (If they have, you can alter their previous voting row) and then add their IP (or username) and vote to the table. To tally, simply get all of the rows from the table and use an array to count the totals. You can then use sort() to sort the results array.

If you would like, I can create a small mockup in PHP for you. If you would provide me with the HTML form, I can even write the demo around it, but that's up to you. My system should be pretty adaptable and easy to implement. You won't have any problem writing you own!

Have a great new year! (And a fantastic Twenty-Ten!)

Ahh...funny enough I did something similar as one of my first large PHP projects. The code I wrote back then was quite atrocious, but its gotten better :P. It was pretty straightforward then, but if at all possible, I would use OOP. Like you said, you only need a backend now, and I would naturally suggest PHP.

Along the lines of how, you need a MySQL table with a rows for id, IP (or username if you have a user system), and vote. The vote value is the most important, since it holds the user's vote (Surprise there!). Anyway, one of the first things I tried was a MySQL table with candidates and total votes stored as a field in their table. This is a NO! There are so many problems with this system; so trust me, make the votes and the candidates separate in MySQL. I suggest a table of votes, with the vote field being an enum with the id of each candidate or the name of their corresponding radio button.

With this, all you will need to do in PHP is validate that the user hasn't already voted (If they have, you can alter their previous voting row) and then add their IP (or username) and vote to the table. To tally, simply get all of the rows from the table and use an array to count the totals. You can then use sort() to sort the results array.

If you would like, I can create a small mockup in PHP for you. If you would provide me with the HTML form, I can even write the demo around it, but that's up to you. My system should be pretty adaptable and easy to implement. You won't have any problem writing you own!

Have a great new year! (And a fantastic Twenty-Ten!)

Oh..I already have my database in MySQL..i had a table already for candidates (id,name of candidate,partyname,position)..And another table for the total votes(id,totalvotes)..

Here's my Html code:

<h2><center>Select Candidates</h2>

<p><table cellspacing=5 cellpadding=5>
<tr>
<th>President:</th>
<tr></tr><td>1.Name presidents&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="pre1">&nbsp;&nbsp;<th><a href="">View Profile</th></a></td>
<tr></tr><td>2.Name presidents&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="pre2">&nbsp;&nbsp;<th><a href="">View Profile</th></a></td>
<tr></tr><td>3.Name presidents&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="pre3">&nbsp;&nbsp;<th><a href="">View Profile</th></a></td>
</tr>
		
<tr>
<th>Vice President:</th>
<tr></tr><td>1.Name vice presidents&nbsp;&nbsp;&nbsp;<input type="radio" name="vice1">&nbsp;&nbsp;<th><a href="">View Profile</th></a></td>
<tr></tr><td>2.Name vice presidents&nbsp;&nbsp;&nbsp;<input type="radio" name="vice2">&nbsp;&nbsp;<th><a href="">View Profile</th></a></td>
<tr></tr><td>3.Name vice presidents&nbsp;&nbsp;&nbsp;<input type="radio" name="vice3">&nbsp;&nbsp;<th><a href="">View Profile</th></a></td>
</tr>

<tr>
<th>Secretary:</th>
<tr></tr><td>1.Name secretary&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="sec1">&nbsp;&nbsp;<th><a href="">View Profile</th></a></td>
					<tr></tr><td>2.Name secretary&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="sec2">&nbsp;&nbsp;<th><a href="">View Profile</th></a></td>
<tr></tr><td>3.Name secretary&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="sec3">&nbsp;&nbsp;<th><a href="">View Profile</th></a></td>
</tr>

<tr>
<th>Treasurer:</th>
<tr></tr><td>1.Name treasurer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="tre1">&nbsp;&nbsp;<th><a href="">View Profile</th></a></td>
<tr></tr><td>2.Name treasurer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="tre2">&nbsp;&nbsp;<th><a href="">View Profile</th></a></td>
<tr></tr><td>3.Name treasurer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="tre3">&nbsp;&nbsp;<th><a href="">View Profile</th></a></td>
</tr>

<tr>
<th>P.R.O/Auditor:</th>
<tr></tr><td>1.Name P.R.O/Auditor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="p1">&nbsp;&nbsp;<th><a href="">View Profile</th></a></td>
					<tr></tr><td>2.Name P.R.O/Auditor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="p2">&nbsp;&nbsp;<th><a href="">View Profile</th></a></td>
<tr></tr><td>3.Name P.R.O/Auditor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="p3">&nbsp;&nbsp;<th><a href="">View Profile</th></a></td>
</tr>

</table></p>
					 
<h2><center><input type="submit" value="     VOTE     "></h2>

Yap..the voter/user should not vote twice..Voter can vote only once or else the system displays that the voter had already voted..tnx Hapi new year! ;) Godbless

So I'm assuming you want the mock-up...I'll get started.
Happy New Year!

So I'm assuming you want the mock-up...I'll get started.
Happy New Year!

here's my sql in table candidates:

CREATE TABLE `candidates` (
  `id` tinyint(4) NOT NULL auto_increment,
  `name` varchar(30) NOT NULL,
  `position` varchar(20) NOT NULL,
  `partyname` varchar(25) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;



INSERT INTO `candidates` VALUES(1, 'Mickey kio', 'President', 'ARCANGEL');
INSERT INTO `candidates` VALUES(2, 'Michael lori', 'Vice President', 'SMART');
INSERT INTO `candidates` VALUES(3, 'lorence Sy', 'Secretary', 'DREAMERS');

and table of totalvotes:

CREATE TABLE `totalvotes` (
  `id` tinyint(4) NOT NULL auto_increment,
  `total` varchar(20) NOT NULL,
  
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

i'll wait for your mock up..tnx for everything.. :) Godbless

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.