poll.php:
-------------------------------------------------------
<?
// Mysql Gegevens
$host = "localhost";
$user = "";
$pass = "";
$db = "";
// Mysql Verbinding maken
$connect = mysql_connect($host, $user, $pass) or die (mysql_error());
mysql_select_db($db,$connect) or die (mysql_error());
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 .= "<b>" . $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 />";
}
# End form
$poll .= "<input type=\"submit\" name=\"vote\" value=\"Stem!\" />";
$poll .= "</form>";
# Show pollid no. and poll date
$poll .= "<p>PollID: " . $data['poll_id'] . " - PollDatum: " . date("j-n-Y", $data['poll_datum']);
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 sql-queries
mysql_query( $sql1 ) or die( mysql_error() );
mysql_query( $sql2 ) or die( mysql_error() );
}
function getPoll()
{
# Select the newest poll
$sql = "SELECT * FROM `poll` ORDER BY `poll_id` DESC LIMIT 1";
# Get array
$q = mysql_query( $sql ) or die( mysql_error() );
$data = mysql_fetch_assoc( $q );
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 .= "<b>" . $data['poll_stelling'] . "</b><br />";
$results .= "Totaal aantal stemmen: " . $votes . "<br />";
$results .= "<p>";
# Loop through results
for($i = 0;$i <= $option; $i++)
{
$results .= $options[$i] . "<br />";
$results .= "<img src=\"indicator.png\" width=\"" . $this->imgPXL($votes, $scores[$i]) . "\" height=\"15\" />" . $this->imgPXL($votes, $scores[$i]) . "%";
$results .= "<br />";
}
$results .= "</p>";
# Show pollid no. and poll date
$results .=" - PollDatum: " . date("j-n-Y", $data['poll_datum']);
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>" . $data['poll_stelling'] . "</b><br />";
$results .= "Totaal aantal stemmen: " . $votes . "<br />";
$results .= "<p>";
# Loop through results
for($i = 0;$i <= $option; $i++)
{
$results .= $options[$i] . "<br />";
$results .= "<img src=\"indicator.png\" width=\"" . $this->imgPXL($votes, $scores[$i]) . "\" height=\"15\" />" . $this->imgPXL($votes, $scores[$i]) . "%";
$results .= "<br />";
}
$results .= "</p>";
# Show pollid no. and poll date
$results .= "<p>PollID: " . $data['poll_id'] . " - PollDatum: " . date("j-n-Y", $data['poll_datum']);
# Show link to archives
$results .= "<p><a href=\"poll_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";
# Get array
$q = mysql_query( $sql ) or die( mysql_error() );
$data = mysql_fetch_assoc( $q );
return $data;
}
# Calculate indicator size
function imgPXL($tot, $opt)
{
$pro = $tot / 100;
$num = $opt / $pro;
return (int) $num;
}
}
?>
-----------------------------------------------
index.php:
<?php
ob_start();
session_start();
include_once("config.php");
include_once("functies.php");
beveiliging(3);
top($config['title'],$config['logo'],$config['bgcolor']);
?>
<table bgcolor="#A1B6C9" style="border: 1px solid #000000" align="center" width="700" height="10" border="0" cellpading="0" cellspacing="2">
<tr>
<td valign="top">
<table width="150" border="0" cellpading="0" cellspacing="0">
<tr>
<td style="border: 1px solid #000000" background="images/caption.png">
- Inloggen
</td>
</tr>
<tr>
<td <? if(isset($_SESSION['zfs_id'])) { } else { echo 'align="center"'; }?> bgcolor="#A1B6C9" style="border: 1px solid #000000">
<?
if(isset($_SESSION['zfs_id']))
{
memberdex($_SESSION['zfs_id']);
}
else
{
loginform();
}
?>
</td>
</tr>
</table>
<table width="150" border="0" cellpading="0" cellspacing="0">
<tr>
<td style="border: 1px solid #000000" background="images/caption.png"> - Poll </td>
</tr>
<tr>
<td bgcolor="#A1B6C9" style="border: 1px solid #000000">
<?
include( 'poll.php' );
$poll = new poll;
$actie = $_GET['actie'];
if( $actie == 'vote' )
{
$poll->updateVotes( $_POST['poll'] );
echo 'De stem is succesvol verwerkt!';
}
else
{
echo $poll->showOptions();
}
?>
hoe kan dat als ik probeer te vote dat hij het niet pakt het lijkt of hij de pagina vernieuwd hoe kan dat? hij pakt de stem niet zeg maar
1.110 views