PHP OOP simple poll system27 Aug 2011 Add Comments Beginner Level
Table Of Content
We will need storage to store voter's votes as well as poll's options. In this case, we will use MySQL as our storage. We will have table "options" to keep available poll options, and table "voters" to keep voters' information ( ip in our case). We assume you have experience of using MySQL, so create database tables as below:
Then, for demonstration purpose, I will insert some data into table "options":
As I have mentioned earlier, our approach is implement this system is OOP. So let us take a look at the PHP object class "Vote":
Firstly, let us take a look at variables in this class:
- private $_host: Host url for MySQL connection.
- private $_database: MySQL database name.
- private $_dbUser: MySQL database user name.
- private $_dbPwd: MySQL database user password.
- private $_con: This stores MySQL connection. And it is created once per object.
- private $_optionTable: options' table name. This makes life easy if you want to use other name instead of "options".
- private $_voterTable: voters' table name.This makes life easy if you want to use other name instead of "voters".
This class is very straight forward if you have some PHP coding knowledge.
There four public functions:
- public function vote($optionId): This function will vote for option with $optionId and it gets voter's ip using $_SERVER['REMOTE_ADDR'].
- public function getList(): This function get array of available options with id and name fields.
- public function showResults(): This function runs a left join to get array of options with its voting results.
- public function getTotal(): This function returns total number of votes for this poll.
Poll page is the actual front page where we make sure of Vote class to implement a poll system.
- Include "vote.php" file, initialize a vote object and a $action string variable.
- Determine if there is action passed to this page, if so, store it to $action variable we have created above.
- Depends on the $action value, we will execute two different codes.
- If $action equals to "vote", firstly call vote() function of Vote class to vote. $id is passed in using URL.
- If $action equals to "vote", secondly call getTotal() function to get total number of results.
- If $action equals to "vote", thirdly call showResults() function to get array of the results, and then loop through each element to out put the result list.
- If $action does not equal to "vote", call function getList() to get available options and loop through each element to form a vote list, and note we append $id and $action with value "vote" to each URL.
You can download this script from my github account.
Thank you for reading this article, and if you have any encountered anything different, have a different solution or think our solution is wrong, do let us know in the comment section.
We will be very happy to hear that.
If you like our tutorial, please follow us on Twitter and help spread the word. We need your support to continue.