Nieuw 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
<?
class poll
{
# Show options function
function showOptions()
{
# Get newest poll
$data = $this->getPoll();
# Split options
$options = explode(",", $data['poll_keuzes']);
# Count options for loop
$aant = count($options)-1;
$poll .= "<h1><b style=\"color:#333333; font-size:17;font-family:Arial, Helvetica, sans-serif;\">" . $data['poll_stelling'] . "</b><br />";
$poll .= "<form method=\"POST\" action=\"" . $_SERVER['PHP_SELF'] . "\">";
# Loop and make input radios
for($i = 0; $i <= $aant; $i++)
{
$j = $i+1;
$poll .= "<input type=\"radio\" value=\">" . $j . "\" name=\"poll\" />" . $options[$i] . "<br />";
}
$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;\"/>";
$poll .= "</form>";
# Show link to archives
# $poll .= "<p><a href=\"archives.php\">Bekijk archief!</a>";
return $poll;
}
function updateVotes($vote)
{
# Get newest poll
$data = $this->getPoll();
# Split the vote scores
$stemmen = explode(",", $data['poll_scores']);
# add one to the selected option
$vote--;
(int) $stemmen[$vote]++;
# Remake the data
$stem = implode(",", $stemmen);
# Add to database
$sql1 = "UPDATE `poll` SET `poll_scores` = '" . $stem . "' WHERE `poll_id` = '" . $data['poll_id'] . "'";
$ip = $_SERVER['REMOTE_ADDR'];
$sql2 = "INSERT INTO `votes` SET `vote_ip` = '" . $ip . "', `vote_datum` = '" . time() . "'";
# Run db class
$db = new database;
$res1 = $db->execute($sql1);
$res2 = $db->execute($sql2);
}
function getPoll()
{
# Select the newest poll
$sql = "SELECT * FROM `poll` ORDER BY `poll_id` DESC LIMIT 1";
# Run db class
$db = new database;
# Get array
$data = $db->get_single($sql);
return $data;
}
function showResults()
{
# Get data
$data = $this->getPoll();
# Split options and scores
$options = explode(",", $data['poll_keuzes']);
$scores = explode(",", $data['poll_scores']);
$option = count($options)-1;
# Count the votes
$votes = array_sum($scores);
# Show total votes
$results .= "<h1><b style=\"color:#333333; font-size:17;font-family:Arial, Helvetica, sans-serif;\">" . $data['poll_stelling'] . "</b><br />";
$results .= "Totaal aantal stemmen: <b style=\"color:#333333\">" . $votes . "</b></h1>";
$results .= "<h1>";
# Loop through results
for($i = 0;$i <= $option; $i++)
{
$results .= $options[$i] . "<br />";
$results .= "<img src=\"images/indicator.png\" width=\"" . $this->imgPXL($votes, $scores[$i]) . "\" height=\"10\"/> " . $this->imgPXL($votes, $scores[$i]). "%";
$results .= "<br />";
}
$results .= "</p>";
# Show link to archives
$results .= "<p><a href=\"archives.php\">Bekijk archief!</a>";
return $results;
}
function archResults($id)
{
# Get data
$data = $this->archPoll($id);
# Split options and scores
$options = explode(",", $data['poll_keuzes']);
$scores = explode(",", $data['poll_scores']);
$option = count($options)-1;
# Count the votes
$votes = array_sum($scores);
# Show total votes
$results .= "<b><h1>" . $data['poll_stelling'] . "</b><br />";
$results .= "<p><h1>";
# Loop through results
for($i = 0;$i <= $option; $i++)
{
$results .= $options[$i] . "<h1><br />";
$results .= "<h1><img src=\"images/indicator.png\" width=\"" . $this->imgPXL($votes, $scores[$i]) . "\" height=\"15\" />" . $this->imgPXL($votes, $scores[$i]) . "%";
$results .= "<br />";
$results .= "<h1>Totaal aantal stemmen: " . $votes . "";
}
$results .= "</p>";
# Show link to archives
$results .= "<p><a href=\"archives.php\">Terug!</a>";
return $results;
}
function archPoll($id)
{
# Select the newest poll
$sql = "SELECT * FROM `poll` WHERE `poll_id` = '" . $id . "' ORDER BY `poll_id` DESC LIMIT 1";
# Run db class
$db = new database;
# Get array
$data = $db->get_single($sql);
return $data;
}
# Calculate indicator size
function imgPXL($tot, $opt)
{
$pro = $tot / 100;
$num = $opt / $pro;
return (int) $num;
}
}
?>
<? class poll { # Show options function function showOptions() { # Get newest poll $data = $this->getPoll(); # Split options $options = explode(",", $data['poll_keuzes']); # Count options for loop $aant = count($options)-1; $poll .= "<h1><b style=\"color:#333333; font-size:17;font-family:Arial, Helvetica, sans-serif;\">" . $data['poll_stelling'] . "</b><br />"; $poll .= "<form method=\"POST\" action=\"" . $_SERVER['PHP_SELF'] . "\">"; # Loop and make input radios for($i = 0; $i <= $aant; $i++) { $j = $i+1; $poll .= "<input type=\"radio\" value=\">" . $j . "\" name=\"poll\" />" . $options[$i] . "<br />"; } $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;\"/>"; $poll .= "</form>"; # Show link to archives # $poll .= "<p><a href=\"archives.php\">Bekijk archief!</a>"; return $poll; } function updateVotes($vote) { # Get newest poll $data = $this->getPoll(); # Split the vote scores $stemmen = explode(",", $data['poll_scores']); # add one to the selected option $vote--; (int) $stemmen[$vote]++; # Remake the data # Add to database $sql1 = "UPDATE `poll` SET `poll_scores` = '" . $stem . "' WHERE `poll_id` = '" . $data['poll_id'] . "'"; $ip = $_SERVER['REMOTE_ADDR']; $sql2 = "INSERT INTO `votes` SET `vote_ip` = '" . $ip . "', `vote_datum` = '" . time() . "'"; # Run db class $db = new database; $res1 = $db->execute($sql1); $res2 = $db->execute($sql2); } function getPoll() { # Select the newest poll $sql = "SELECT * FROM `poll` ORDER BY `poll_id` DESC LIMIT 1"; # Run db class $db = new database; # Get array $data = $db->get_single($sql); return $data; } function showResults() { # Get data $data = $this->getPoll(); # Split options and scores $options = explode(",", $data['poll_keuzes']); $scores = explode(",", $data['poll_scores']); $option = count($options)-1; # Count the votes # Show total votes $results .= "<h1><b style=\"color:#333333; font-size:17;font-family:Arial, Helvetica, sans-serif;\">" . $data['poll_stelling'] . "</b><br />"; $results .= "Totaal aantal stemmen: <b style=\"color:#333333\">" . $votes . "</b></h1>"; $results .= "<h1>"; # Loop through results for($i = 0;$i <= $option; $i++) { $results .= $options[$i] . "<br />"; $results .= "<img src=\"images/indicator.png\" width=\"" . $this->imgPXL($votes, $scores[$i]) . "\" height=\"10\"/> " . $this->imgPXL($votes, $scores[$i]). "%"; $results .= "<br />"; } $results .= "</p>"; # Show link to archives $results .= "<p><a href=\"archives.php\">Bekijk archief!</a>"; return $results; } function archResults($id) { # Get data $data = $this->archPoll($id); # Split options and scores $options = explode(",", $data['poll_keuzes']); $scores = explode(",", $data['poll_scores']); $option = count($options)-1; # Count the votes # Show total votes $results .= "<b><h1>" . $data['poll_stelling'] . "</b><br />"; $results .= "<p><h1>"; # Loop through results for($i = 0;$i <= $option; $i++) { $results .= $options[$i] . "<h1><br />"; $results .= "<h1><img src=\"images/indicator.png\" width=\"" . $this->imgPXL($votes, $scores[$i]) . "\" height=\"15\" />" . $this->imgPXL($votes, $scores[$i]) . "%"; $results .= "<br />"; $results .= "<h1>Totaal aantal stemmen: " . $votes . ""; } $results .= "</p>"; # Show link to archives $results .= "<p><a href=\"archives.php\">Terug!</a>"; return $results; } function archPoll($id) { # Select the newest poll $sql = "SELECT * FROM `poll` WHERE `poll_id` = '" . $id . "' ORDER BY `poll_id` DESC LIMIT 1"; # Run db class $db = new database; # Get array $data = $db->get_single($sql); return $data; } # Calculate indicator size function imgPXL($tot, $opt) { $pro = $tot / 100; $num = $opt / $pro; return (int) $num; } } ?>
Pollbeheer.php (toont de poll op deze site.
<?
# Include database classfile
include("database.php");
# Include poll classfile
include("poll.php");
# Run database class
$db = new database;
# Set query to see if voted already
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "SELECT * FROM `votes` WHERE `vote_ip` = '" . $ip . "'";
# Run poll class
$poll = new poll;
# If a match is found show results only
if($db->num_rows($sql) == 1)
{
# Show results
echo $poll->showResults();
}
else
{
# Check if form submitted
if(!$_POST['vote'])
{
# Show form
echo $poll->showOptions();
}
else
{
# Add data to database
# Check for a value
if(!$_POST['poll'])
{
# No option selected
# Show poll again
echo $poll->showOptions();
}
else
{
# A radio is checked
# Update votes
$poll->updateVotes($_POST['poll']);
# Show results
echo $poll->showResults();
}
}
}
?>
<? # Include database classfile include("database.php"); # Include poll classfile include("poll.php"); # Run database class $db = new database; # Set query to see if voted already $ip = $_SERVER['REMOTE_ADDR']; $sql = "SELECT * FROM `votes` WHERE `vote_ip` = '" . $ip . "'"; # Run poll class $poll = new poll; # If a match is found show results only if($db->num_rows($sql) == 1) { # Show results echo $poll->showResults(); } else { # Check if form submitted if(!$_POST['vote']) { # Show form echo $poll->showOptions(); } else { # Add data to database # Check for a value if(!$_POST['poll']) { # No option selected # Show poll again echo $poll->showOptions(); } else { # A radio is checked # Update votes $poll->updateVotes($_POST['poll']); # Show results echo $poll->showResults(); } } } ?>
hopelijk kan iemand helpen
|