login  Naam:   Wachtwoord: 
Registreer je!
 Forum

Poll telt niet (Opgelost)

Offline Designick - 25/03/2009 18:45
Avatar van DesignickNieuw lid Hallo,

ik heb een probleem met een poll.
Hij doet alles. Telt in database etc etc.

Alleen hij laat de uiteindelijke %ten niet zien.
Heel raar en zie niet wat er fout is.

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


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


hopelijk kan iemand helpen

4 antwoorden

Gesponsorde links
Offline mark92 - 25/03/2009 18:54
Avatar van mark92 Gouden medaille

Nieuw lid
Misschien werkt dit:
  1. function imgPXL($tot, $opt)
  2. {
  3. $num = $opt / $tot * 100;
  4.  
  5. return round($num);
  6. }
Offline Designick - 25/03/2009 18:57
Avatar van Designick Nieuw lid
mark92 schreef:
Misschien werkt dit:
[..code..]


Werkt niet, sorry.
Toch bedankt
Offline mark92 - 25/03/2009 19:02 (laatste wijziging 25/03/2009 19:04)
Avatar van mark92 Gouden medaille

Nieuw lid
ik zie het al:
  1. $scores = explode(",", $data['poll_scores']);
  2. (...)
  3. $votes = array_sum($scores);

je gebruikt array_sum op een array met alleen maar strings erin. De optelsom wordt 0, aangezien deze functie alleen integers en floats optelt.
Misschien verhelpt dit het probleem, alleen weet ik het niet zeker:
  1. $scores = (float) explode(",", $data['poll_scores']);

Anders zou je ze 'handmatig' op moeten tellen..

EDIT: Volgens mij ben ik er niet zo bij vandaag  
Offline Designick - 25/03/2009 19:18
Avatar van Designick Nieuw lid
mark92 schreef:
ik zie het al:
[..code..]
je gebruikt array_sum op een array met alleen maar strings erin. De optelsom wordt 0, aangezien deze functie alleen integers en floats optelt.
Misschien verhelpt dit het probleem, alleen weet ik het niet zeker:
[..code..]
Anders zou je ze 'handmatig' op moeten tellen..

EDIT: Volgens mij ben ik er niet zo bij vandaag  


Nee werkt ook niet,
maar ik heb het orgineel nog gevonden tussen me backups en die werkt wel ;) Toch bedankt voor je inzet!
Gesponsorde links
Dit onderwerp is gesloten.
Actieve forumberichten
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.232s