This easy-to-use class library enables you to set up your own survey system in just few minutes. Package includes dynamically generated form (with number of total votes), detailed result page, view of old polls, administration page, check for repeated votes and virtually unlimited number of questions.
By : tebrino
*********************************************************************************** Simple Mini Poll class library (SimPoll)
This easy-to-use class library enables you to set up your own survey system in just few minutes. Package includes dynamically generated form (with number of total votes), detailed result page, view of old polls, administration page, check for repeated votes. It's free for all purposes, just please don't claim you wrote it and if you like it and find it useful please leave link on results page. If you have any problems, please feel free to contact me. Also if you use it, please send me the page URL.
INSTRUCTIONS:
1. Execute these queries on your database:
# Table structure for table `poll_check`
CREATE TABLE `poll_check` ( `pollid` int(11) NOT NULL default '0', `ip` varchar(20) NOT NULL default '', `time` varchar(14) NOT NULL default '' ) TYPE=MyISAM COMMENT='';
2. Copy and paste code bellow in separate files and name them as noted 3. Set up database connection parameters in file config.php 4. Optionally you can change layout of form and results page 5. Create new poll by starting admin page (in this case test_poll_admin.php) 6. Activate your poll by clicking ACTIVATE 7. Enjoy and send me your comments
*********************************************************************************** CLASS FILES:
file miniPoll.class.php *********************************************************************************** <?php class miniPoll {
var $show_vote_count; var $active_poll_id; var $active_poll_title; var $timestamp; var $timeout; var $ip; var $repeated_vote; var $results_page; var $old_polls;
function miniPoll() { $this->show_vote_count = true; // display total votes? true/false $this->pollLayout(); $this->getActivePoll(); $this->timestamp = time(); $this->timeout = $this->timestamp - 1800; $this->ip = $_SERVER['REMOTE_ADDR']; $this->repeated_vote = "You already voted today<br />"; $this->results_page = "test_poll_results.php"; // page where you display results $this->old_polls = true; // if true enables view of old polls. this only display old polls it doesn't allow users to vote. true/false
}
function pollLayout() { // it allows you to set visual settings using CSS definitions included in file where you're calling this class // replace these with your own CSS styles $this->form_table_width = "120px"; $this->form_title = "menuhd"; $this->form_table = "tabele"; $this->form_table_cell = "poll"; $this->form_button = "formlook"; $this->poll_question = "fat"; // this is for <span> tag $this->results_title = "menuhd"; $this->results_table = ""; $this->results_poll_question = "fat"; $this->result_table_width = "450px"; $this->result_table_cell = "pollbg"; $this->bar_image = "images/bar.jpg"; // please select 1px width x 15px height image }
function getActivePoll() { $sql = @mysql_query ("SELECT pollid, polltitle FROM poll_desc WHERE status = 'active'"); $row = @mysql_fetch_object($sql); $this->active_poll_id = $row->pollid; $this->active_poll_title = $row->polltitle; return; }
function voteCount() { $sql = @mysql_query ("SELECT SUM(votecount) AS votecount FROM poll_data WHERE pollid = '$this->active_poll_id'"); $row = @mysql_fetch_object($sql); $this->votecount = $row->votecount; return $this->votecount; }
function pollForm() { $sql = @mysql_query ("SELECT polltext, voteid FROM poll_data WHERE pollid = '$this->active_poll_id' ORDER BY voteid"); if (@mysql_num_rows($sql) > 0) { echo "<table width=\"$this->form_table_width\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" class=\"$this->form_table\"> <tr><td class=\"$this->form_title\">Mini Poll</td></tr> <tr><td class=\"$this->form_table_cell\">\r\n"; echo "<form action=\"$this->results_page\" name=\"pollf\" id=\"pollf\" method=\"get\"> <span class=\"$this->poll_question\">" . $this->active_poll_title . "</span><br />\r\n";
while ($row = @mysql_fetch_object($sql)) { if (!empty($row->polltext)) { echo "\t<input type=\"radio\" name=\"voteid\" value=\"$row->voteid\" /> $row->polltext <br />\r\n"; } }
function deleteCheck() { $sql = @mysql_query ("DELETE FROM poll_check WHERE time < '$this->timeout'"); return; }
function insertCheck() { $sql = @mysql_query ("INSERT INTO poll_check (ip, time) VALUES ('$this->ip', '$this->timestamp')"); return; }
function voteCheck() { $this->deleteCheck(); $sql = @mysql_query ("SELECT ip FROM poll_check WHERE ip = '$this->ip'"); if (@mysql_num_rows($sql) == 0) { $this->insertCheck(); return true; } else { return false; } }
function processPoll($pollid, $voteid) { if ($this->voteCheck()) { $sql = @mysql_query ("UPDATE poll_data SET votecount = votecount + 1 WHERE voteid = '$voteid' AND pollid = '$pollid'"); } else { echo $this->repeated_vote; }
}
function selectedPoll($pollid) { $sql = @mysql_query ("SELECT polltitle FROM poll_desc WHERE pollid = '$pollid'"); $row = @mysql_fetch_object($sql); $this->polltitle = $row->polltitle; return $this->polltitle; }
function selectedPollVotecount($pollid) { $sql = @mysql_query ("SELECT SUM(votecount) AS votecount FROM poll_data WHERE pollid = '$pollid'"); $row = @mysql_fetch_object($sql); $this->votecount = $row->votecount; return $this->votecount; }
function formatDate($val) { $arr = explode("-", $val); return date("d. F Y.", mktime (0,0,0, $arr[1], $arr[2], $arr[0])); }
function oldPolls($pollid) { $sql = mysql_query ("SELECT pollid, polltitle, timestamp FROM poll_desc WHERE pollid <> '$pollid'"); if (mysql_num_rows($sql) > 0) { echo "<tr><td class=\"$this->result_table_cell\" colspan=\"2\">\r\n"; while ($row = mysql_fetch_object($sql)) { $datum = $this->formatDate($row->timestamp); echo "<a href=\"$this->results_page?pollid=$row->pollid\">$row->polltitle</a> $datum<br />\r\n"; } echo "</td></tr>\r\n"; } }
function pollResults($pollid) { $this->selectedPoll($pollid); $this->selectedPollVotecount($pollid); $sql = @mysql_query ("SELECT polltext, votecount FROM poll_data WHERE pollid = '$pollid' AND polltext <> ''"); echo "<table border=\"0\" width=\"$this->result_table_width\" class=\"$this->results_table\"> <tr><td class=\"$this->results_title\" colspan=\"2\">Mini Poll Results</td></tr>"; if (@mysql_num_rows($sql) > 0) { echo "<tr><td class=\"$this->results_poll_question\" colspan=\"2\">$this->polltitle</td></tr>\r\n"; while ($row = mysql_fetch_object($sql)) { if ($this->votecount == 0) { $tmp_votecount = 1; } else { $tmp_votecount = $this->votecount; } $vote_percents = number_format(($row->votecount / $tmp_votecount * 100), 2); $image_width = intval($vote_percents * 3); echo "<tr><td class=\"$this->result_table_cell\">$row->polltext $row->votecount votes. ($vote_percents %)</td><td class=\"$this->result_table_cell\"> <img src=\"$this->bar_image\" width=\"$image_width\" alt=\"$vote_percents %\" height=\"15\" /> </td></tr>\r\n"; } echo "<tr><td class=\"$this->result_table_cell\" colspan=\"2\">Total votes: $this->votecount</td></tr>\r\n"; } if ($this->old_polls) { $this->oldPolls($pollid); } echo "</table>\r\n"; // if you like this software and you find it useful, please don't remove this link echo "Powered by: <a href=\"http://www.free-midi.org/scripts/\">SimPoll</a> v.1.0\r\n";
function createPoll($pollname, $q) { $this->getLastPollId(); $insert_title = @mysql_query ("INSERT INTO poll_desc(pollid, polltitle, timestamp) VALUES ('$this->maxpoll', '$pollname', now())"); for ($i = 1; $i <= count($q); $i ++) { $insert_questions = @mysql_query ("INSERT INTO poll_data(pollid, polltext, voteid) VALUES ('$this->maxpoll', '$q[$i]', '$i')"); }
}
function activatePoll($pollid) { $deactivate_poll = @mysql_query ("UPDATE poll_desc SET status = '' WHERE status = 'active'"); $activate_poll = @mysql_query ("UPDATE poll_desc SET status = 'active' WHERE pollid = '$pollid'"); if (mysql_affected_rows() > 0) { echo "Poll successfully activated<br />\r\n"; } }
function deletePoll($pollid) { $delete_poll = @mysql_query ("DELETE FROM poll_desc WHERE pollid = '$pollid'"); if (mysql_affected_rows() > 0) { $delete_poll_questions = @mysql_query ("DELETE FROM poll_data WHERE pollid = '$pollid'"); echo "Poll successfully deleted.<br />\r\n"; } }
file test_poll.php *********************************************************************************** <?php error_reporting(E_ALL); // when you finish testing you should change this to E_NONE
file test_poll_results.php - results page *********************************************************************************** <?php error_reporting(E_ALL); // when you finish testing you should change this to E_NONE
file poll.css - example style sheet *********************************************************************************** /* Example class for Mini Poll */
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.