login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Poll/Stem systemen > PRO poll !!!

PRO poll !!!

Auteur: sayanora - 25 mei 2005 - 20:24 - Gekeurd door: Mokka - Hits: 27424 - Aantal punten: 4.19 (27 stemmen)




Over PRO PoLL :
- Adminfunctie
- Archieffunctie
- Makkelijke installatie middels instal.php
- Radios worden automatisch gemaakt aan de hand van het aantal antwoorden, maw er zijn erg veel antwoorden mogelijk.
- Ip logging zodat er per poll maar 1x gestemd kan worden
- Grafische en procentuele indicator
- Polls makkelijk te herkennen aan weergegeven nummer en bijhorende datum

Installatie:
- Download of kopier de bestanden naar een map (bijv. poll)
- Verander in database.php de gegevens zodat ze op jouw db van kracht zijn.
- Open de browser en ga naar .../poll/instal.php en de SQL tabellen worden weggeschreven Let op! als de tabellen poll en/of votes al bestaan worden deze overschreven!
- Met de browser nog open ga je naar .../poll/admin.php en type je de vraag en de antwoorden gescheiden door een comma.
- Je poll is nu operationeel!

Code:
  1. <?
  2. # Filename : admin.php
  3. # Author : Sander Geerts
  4. # Projectname : PRO PoLL
  5. # Projectversion : 0.7.1
  6. # Releasedate : 16-08-2004
  7.  
  8. # Include database classfile
  9. include("database.php");
  10.  
  11. # Class new_poll
  12. class new_poll
  13. {
  14. function showAdmin()
  15. {
  16. $admin .= "<form method=\"POST\" ation=\"" . $_SERVER['PHP_SELF'] . "\">";
  17. $admin .= "Vraag :<br />";
  18. $admin .= "<input type=\"text\" name=\"q\" maxlength=\"255\" /><br />";
  19. $admin .= "Antwoorden : (gescheiden door een komma en zonder spaties)<br />";
  20. $admin .= "<input type=\"text\" name=\"a\" /><br />";
  21. $admin .= "<input type=\"submit\" name=\"add\" value=\"Voeg toe!\" />";
  22. $admin .= "</form>";
  23.  
  24. return $admin;
  25. }
  26.  
  27. function makeScores($num)
  28. {
  29. for($i=1;$i<=$num;$i++)
  30. {
  31. if($i == $num)
  32. {
  33. $scores .= "0";
  34. }
  35. else
  36. {
  37. $scores .= "0,";
  38. }
  39. }
  40. return $scores;
  41. }
  42. }
  43.  
  44. # Check if user is Admin
  45. # Hier kun je je eigen log systeem in verwerken
  46. $admin = 1;
  47. if($admin)
  48. {
  49. # Show options
  50. $np = new new_poll;
  51. $db = new database;
  52.  
  53. # Check if form is submitted
  54. if(!$_POST['add'])
  55. {
  56. # Show admin
  57. echo $np->showAdmin();
  58. }
  59. else
  60. {
  61. # Set some vars
  62. $a = $_POST['a'];
  63. $total = count(explode(",", $a));
  64. $q = htmlentities($_POST['q']);
  65. $a = $_POST['a'];
  66. # calc null values
  67. $null = $np->makeScores($total);
  68.  
  69. # Insert new poll
  70. $sql1 = "INSERT INTO `poll` SET `poll_stelling` = '" . $q . "', `poll_keuzes` = '" . $a . "', `poll_scores` = '" . $null . "', `poll_datum` = '" . time() . "'";
  71. # Reset votes
  72. $sql2 = "DELETE FROM `votes`";
  73.  
  74. $res1 = $db->execute($sql1);
  75. $res2 = $db->execute($sql2);
  76.  
  77. echo "Nieuwe poll toegevoegd!";
  78. }
  79. }
  80. else
  81. {
  82. # User isnt an admin
  83. die("INVALID ACCESLEVEL!");
  84. }
  85.  
  86. ?>



  1. <?
  2. # Filename : archives.php
  3. # Author : Sander Geerts
  4. # Projectname : PRO PoLL
  5. # Projectversion : 0.7.1
  6. # Releasedate : 16-08-2004
  7.  
  8. # Include database classfile
  9. include("database.php");
  10. # Include poll classfile
  11. include("poll.php");
  12.  
  13. # Archives class
  14. class archive
  15. {
  16. function showAll()
  17. {
  18. # Run database class
  19. $db = new database;
  20.  
  21. # Get the number of rows
  22. $sql = "SELECT * FROM `poll` ORDER BY `poll_id` DESC";
  23. $aantal = $db->num_rows($sql);
  24.  
  25. $archives .= "<b>Er zijn " . $aantal . " polls in het archief!</b><br />";
  26.  
  27. # Get all rowvalues
  28. $data = $db->get_array($sql);
  29.  
  30. # Set paragraph start
  31. $archives .= "<p>";
  32.  
  33. # Loop to make the URLs
  34. for($i=0; $i<$aantal; $i++)
  35. $archives .= "<a href=\"" . $_SERVER['PHP_SELF'] . "?ArchID=" . $data[$i]['poll_id'] . "\">" . date("j-n-Y", $data[$i]['poll_datum']) . "</a><br />";
  36.  
  37. # End paragraph
  38. $archives .= "</p>";
  39.  
  40. # Link back to mainpage
  41. $archives .= "<p><a href=\"index.php\">Terug</a>";
  42.  
  43. return $archives;
  44. }
  45.  
  46. function showSingle($id)
  47. {
  48. # Run poll class
  49. $poll = new poll;
  50.  
  51. # Get the results
  52. $single = $poll->archResults($id);
  53.  
  54. return $single;
  55. }
  56. }
  57.  
  58. # Run archive class
  59. $ac = new archive;
  60.  
  61. # Check for $_GET data
  62. if($_GET['ArchID'] && is_numeric($_GET['ArchID']))
  63. echo $ac->showSingle($_GET['ArchID']);
  64. else
  65. echo $ac->showAll();
  66. ?>

  1. <?
  2. # Filename : database.php
  3. # Author : Sander Geerts
  4. # Projectname : PRO PoLL
  5. # Projectversion : 0.7.1
  6. # Releasedate : 16-08-2004
  7.  
  8. class database
  9. {
  10. # Database vars
  11. var $user = "User";
  12. var $pass = "Pass";
  13. var $host = "localhost";
  14. var $name = "DBname";
  15.  
  16. # Functie die mysql_num_rows teruggeeft
  17. function num_rows($sql)
  18. {
  19. $this->connect();
  20. $res = mysql_query($sql);
  21. $aantal = mysql_num_rows($res);
  22.  
  23. return $aantal;
  24. }
  25.  
  26. # Functie de mysql_fetch_array teruggeeft
  27. function get_array($sql)
  28. {
  29. $this->connect();
  30. $res = mysql_query($sql);
  31. while($data = mysql_fetch_array($res))
  32. $row[] = $data;
  33.  
  34.  
  35. return $row;
  36. }
  37.  
  38. # Zelfde als hierboven maar dan 1 row
  39. function get_single($sql)
  40. {
  41. $this->connect();
  42. $res = mysql_query($sql);
  43. $data = mysql_fetch_array($res);
  44.  
  45. return $data;
  46. }
  47.  
  48. # Functie die een normale query runt
  49. function execute($sql)
  50. {
  51. $this->connect();
  52. $res = mysql_query($sql);
  53.  
  54. return $res;
  55. }
  56.  
  57. # Functie die de database connectie maakt
  58. function connect()
  59. {
  60. return mysql_select_db($this->name, mysql_connect($this->host, $this->user, $this->pass));
  61. }
  62. }
  63. ?>

  1. <?
  2. # Filename : index.php
  3. # Author : Sander geerts
  4. # Projectname : PRO PoLL
  5. # Projectversion : 0.7.1
  6. # Releasedate : 16-08-2004
  7.  
  8. # Include database classfile
  9. include("database.php");
  10. # Include poll classfile
  11. include("poll.php");
  12.  
  13. # Run database class
  14. $db = new database;
  15.  
  16. # Set query to see if voted already
  17. $ip = $_SERVER['REMOTE_ADDR'];
  18. $sql = "SELECT * FROM `votes` WHERE `vote_ip` = '" . $ip . "'";
  19.  
  20. # Run poll class
  21. $poll = new poll;
  22.  
  23. # If a match is found show results only
  24. if($db->num_rows($sql) == 1)
  25. {
  26. # Show results
  27. echo $poll->showResults();
  28. }
  29. else
  30. {
  31. # Check if form submitted
  32. if(!$_POST['vote'])
  33. {
  34. # Show form
  35. echo $poll->showOptions();
  36. }
  37. else
  38. {
  39. # Add data to database
  40. # Check for a value
  41. if(!$_POST['poll'])
  42. {
  43. # No option selected
  44. # Show poll again
  45. echo $poll->showOptions();
  46. }
  47. else
  48. {
  49. # A radio is checked
  50. # Update votes
  51. $poll->updateVotes($_POST['poll']);
  52.  
  53. # Show results
  54. echo $poll->showResults();
  55. }
  56. }
  57. }
  58. ?>


  1. <?
  2. # Filename : install.php
  3. # Author : Sander Geerts
  4. # Projectname : PRO PoLL
  5. # Projectversion : 0.7.1
  6. # Releasedate : 16-08-2004
  7.  
  8. # Include database classfile
  9. include("database.php");
  10.  
  11. # Instal class
  12. class install
  13. {
  14. # Instal function
  15. function insPoll()
  16. {
  17. $sql1 .= "DROP TABLE IF EXISTS `poll`;";
  18.  
  19. $sql2 .= "CREATE TABLE `poll` (";
  20. $sql2 .= "`poll_id` int(11) primary key auto_increment,";
  21. $sql2 .= "`poll_stelling` varchar(255) not null default '',";
  22. $sql2 .= "`poll_keuzes` longtext not null default '',";
  23. $sql2 .= "`poll_scores` longtext not null default '',";
  24. $sql2 .= "`poll_datum` int(20) not null default '0'";
  25. $sql2 .= ") TYPE=MyISAM;";
  26.  
  27. $sql3 .= "DROP TABLE IF EXISTS `votes`;";
  28.  
  29. $sql4 .= "CREATE TABLE `votes` (";
  30. $sql4 .= "`vote_id` int(11) primary key auto_increment,";
  31. $sql4 .= "`vote_ip` varchar(16) not null default '',";
  32. $sql4 .= "`vote_datum` int(20) not null default '0'";
  33. $sql4 .= ") TYPE=MyISAM;";
  34.  
  35. # Run db class
  36. $db = new database;
  37.  
  38. # Execute
  39. $res1 = $db->execute($sql1);
  40. $res2 = $db->execute($sql2);
  41. $res3 = $db->execute($sql3);
  42. $res4 = $db->execute($sql4);
  43.  
  44. if($res2 && $res4)
  45. {
  46. $install .= "<b>Succesvol geinstalleerd!</b><br />";
  47. $install .= "<a href=\"admin.php\">Klik hier om een vraag in te stellen!</a>";
  48. }
  49. else
  50. {
  51. $install .= "<b>Er is iets fout gegegaan probeer het opnieuw!</b><br />";
  52. $install .= $this->insMenu();
  53. $install .= "<p><b>Is dit al de zoveelste keer dat het mislukt?</b><br />";
  54. $install .= "Voer dan handmatig deze code in in je db:<br />";
  55. $install .= $sql2 . $sql4;
  56. }
  57.  
  58. return $install;
  59. }
  60.  
  61. # Show menu
  62. function insMenu()
  63. {
  64. $menu .= "<p><b>SMART PoLL 0.7 INSTALLER</b><br />";
  65. $menu .= "<u>LeT oP!</u> zodra je op <i>install</i> klikt zullen de tabellen genaamd<br />";
  66. $menu .= "<i>'poll'</i> en <i>'votes'</i>, mits ze al bestaan, overschreven worden!<br />";
  67. $menu .= "<a href=\"" . $_SERVER['PHP_SELF'] . "?ins=1\">INSTALL SMART PoLL 0.7 NOW!</a></p>";
  68.  
  69. return $menu;
  70. }
  71. }
  72.  
  73. # Run install class
  74. $ins = new install;
  75.  
  76. # Check for $_GET data
  77. if($_GET['ins'] && is_numeric($_GET['ins']))
  78. echo $ins->insPoll();
  79. else
  80. echo $ins->insMenu();
  81.  
  82. ?>

  1. <?
  2. # Filename : poll.php
  3. # Author : Sander Geerts
  4. # Projectname : PRO PoLL
  5. # Projectversion : 0.7.1
  6. # Releasedate : 16-08-2004
  7.  
  8. class poll
  9. {
  10. # Show options function
  11. function showOptions()
  12. {
  13. # Get newest poll
  14. $data = $this->getPoll();
  15.  
  16. # Split options
  17. $options = explode(",", $data['poll_keuzes']);
  18. # Count options for loop
  19. $aant = count($options)-1;
  20.  
  21. $poll .= "<b>" . $data['poll_stelling'] . "</b><br />";
  22. $poll .= "<form method=\"POST\" action=\"" . $_SERVER['PHP_SELF'] . "\">";
  23. # Loop and make input radios
  24. for($i = 0; $i <= $aant; $i++)
  25. {
  26. $j = $i+1;
  27. $poll .= "<input type=\"radio\" value=\"" . $j . "\" name=\"poll\" />" . $options[$i] . "<br />";
  28. }
  29. # End form
  30. $poll .= "<input type=\"submit\" name=\"vote\" value=\"Stem!\" />";
  31. $poll .= "</form>";
  32.  
  33. # Show pollid no. and poll date
  34. $poll .= "<p>PollID: " . $data['poll_id'] . " - PollDatum: " . date("j-n-Y", $data['poll_datum']);
  35.  
  36. # Show link to archives
  37. $poll .= "<p><a href=\"archives.php\">Bekijk archief!</a>";
  38.  
  39. return $poll;
  40. }
  41.  
  42. function updateVotes($vote)
  43. {
  44. # Get newest poll
  45. $data = $this->getPoll();
  46.  
  47. # Split the vote scores
  48. $stemmen = explode(",", $data['poll_scores']);
  49.  
  50. # add one to the selected option
  51. $vote--;
  52. (int) $stemmen[$vote]++;
  53.  
  54. # Remake the data
  55. $stem = implode(",", $stemmen);
  56.  
  57. # Add to database
  58. $sql1 = "UPDATE `poll` SET `poll_scores` = '" . $stem . "' WHERE `poll_id` = '" . $data['poll_id'] . "'";
  59. $ip = $_SERVER['REMOTE_ADDR'];
  60. $sql2 = "INSERT INTO `votes` SET `vote_ip` = '" . $ip . "', `vote_datum` = '" . time() . "'";
  61.  
  62. # Run db class
  63. $db = new database;
  64.  
  65. $res1 = $db->execute($sql1);
  66. $res2 = $db->execute($sql2);
  67.  
  68. }
  69.  
  70. function getPoll()
  71. {
  72. # Select the newest poll
  73. $sql = "SELECT * FROM `poll` ORDER BY `poll_id` DESC LIMIT 1";
  74.  
  75. # Run db class
  76. $db = new database;
  77.  
  78. # Get array
  79. $data = $db->get_single($sql);
  80.  
  81. return $data;
  82. }
  83.  
  84. function showResults()
  85. {
  86. # Get data
  87. $data = $this->getPoll();
  88.  
  89. # Split options and scores
  90. $options = explode(",", $data['poll_keuzes']);
  91. $scores = explode(",", $data['poll_scores']);
  92.  
  93. $option = count($options)-1;
  94.  
  95. # Count the votes
  96. $votes = array_sum($scores);
  97.  
  98. # Show total votes
  99. $results .= "<b>" . $data['poll_stelling'] . "</b><br />";
  100. $results .= "Totaal aantal stemmen: " . $votes . "<br />";
  101. $results .= "<p>";
  102.  
  103. # Loop through results
  104. for($i = 0;$i <= $option; $i++)
  105. {
  106. $results .= $options[$i] . "<br />";
  107. $results .= "<img src=\"indicator.png\" width=\"" . $this->imgPXL($votes, $scores[$i]) . "\" height=\"15\" />" . $this->imgPXL($votes, $scores[$i]) . "%";
  108. $results .= "<br />";
  109. }
  110. $results .= "</p>";
  111.  
  112. # Show pollid no. and poll date
  113. $results .= "<p>PollID: " . $data['poll_id'] . " - PollDatum: " . date("j-n-Y", $data['poll_datum']);
  114.  
  115. # Show link to archives
  116. $results .= "<p><a href=\"archives.php\">Bekijk archief!</a>";
  117.  
  118. return $results;
  119. }
  120.  
  121. function archResults($id)
  122. {
  123. # Get data
  124. $data = $this->archPoll($id);
  125.  
  126. # Split options and scores
  127. $options = explode(",", $data['poll_keuzes']);
  128. $scores = explode(",", $data['poll_scores']);
  129.  
  130. $option = count($options)-1;
  131.  
  132. # Count the votes
  133. $votes = array_sum($scores);
  134.  
  135. # Show total votes
  136. $results .= "<b>" . $data['poll_stelling'] . "</b><br />";
  137. $results .= "Totaal aantal stemmen: " . $votes . "<br />";
  138. $results .= "<p>";
  139.  
  140. # Loop through results
  141. for($i = 0;$i <= $option; $i++)
  142. {
  143. $results .= $options[$i] . "<br />";
  144. $results .= "<img src=\"indicator.png\" width=\"" . $this->imgPXL($votes, $scores[$i]) . "\" height=\"15\" />" . $this->imgPXL($votes, $scores[$i]) . "%";
  145. $results .= "<br />";
  146. }
  147. $results .= "</p>";
  148.  
  149. # Show pollid no. and poll date
  150. $results .= "<p>PollID: " . $data['poll_id'] . " - PollDatum: " . date("j-n-Y", $data['poll_datum']);
  151.  
  152. # Show link to archives
  153. $results .= "<p><a href=\"archives.php\">Terug!</a>";
  154.  
  155. return $results;
  156. }
  157.  
  158. function archPoll($id)
  159. {
  160. # Select the newest poll
  161. $sql = "SELECT * FROM `poll` WHERE `poll_id` = '" . $id . "' ORDER BY `poll_id` DESC LIMIT 1";
  162.  
  163. # Run db class
  164. $db = new database;
  165.  
  166. # Get array
  167. $data = $db->get_single($sql);
  168.  
  169. return $data;
  170. }
  171.  
  172. # Calculate indicator size
  173. function imgPXL($tot, $opt)
  174. {
  175. $pro = $tot / 100;
  176. $num = $opt / $pro;
  177.  
  178. return (int) $num;
  179. }
  180. }
  181. ?>


veel geluk ermeeDownload code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (25)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.15s