[config.php]
[code]
<?php

$dbhost = 'localhost';
$dbname = 'destruin_forum';
$dbuser = 'root';
$dbpasswd = 'flikkerop';

?>
[/code]

[database]
CREATE TABLE `stelling` (
  `id` int(11) NOT NULL auto_increment,
  `count` int(11) NOT NULL default '0',
  `titel` varchar(30) collate utf8_unicode_ci NOT NULL default '',
  `vraag` varchar(90) collate utf8_unicode_ci NOT NULL default '',
  `antwoord1` varchar(60) collate utf8_unicode_ci NOT NULL default '',
  `antwoord2` varchar(60) collate utf8_unicode_ci NOT NULL default '',
  `antwoord3` varchar(60) collate utf8_unicode_ci NOT NULL default '',
  `item1` int(11) NOT NULL default '0',
  `item2` int(11) NOT NULL default '0',
  `item3` int(11) NOT NULL default '0',
  `datum` int(11) NOT NULL default '0',
  `active` varchar(6) collate utf8_unicode_ci NOT NULL default 'false',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=43 ;


CREATE TABLE `stelling_votes` (
  `id` int(11) NOT NULL auto_increment,
  `user_id` int(11) NOT NULL default '0',
  `vraag_id` int(11) NOT NULL default '0',
  `antwoord_id` int(11) NOT NULL default '0',
  `datum` int(11) NOT NULL default '0',
  `ip` varchar(40) collate utf8_unicode_ci NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=576 ;

[pollClass.php]
[code]
<?php

	function wrap($tekst, $maxlen) {
		return wordwrap(stripslashes($tekst), $maxlen, "<br />\n", true);
	}

	class minipoll {

		var $poll;
		var $count;
		var $handle;
		var $ident;

		//var $dbhost;
		var $dbname;
		//var $dbuser;
		//var $dbpasswd;

		// Init en lees record in
		function minipoll($dbhost,$dbname,$dbuser,$dbpasswd)
		{
			$handle = mysql_connect($dbhost, $dbuser, $dbpasswd);
    		mysql_select_db($dbname, $handle);
		  	$sql = "SELECT * FROM stelling WHERE active='true'";
			$result = mysql_query($sql);
			$this->count = mysql_num_rows($result);
			$this->poll = mysql_fetch_array($result);
			$this->ident = $this->poll['id'];
			$this->handle = $handle;
			//$this->dbhost = $dbhost;
			$this->dbname = $dbname;
			//$this->dbuser = $dbuser;
			//$this->dbpasswd = $dbpasswd;

		}

		// Ophalen record voor gebruik
		function pollGet()
		{
			//echo '<pre>'.print_r($this->poll, true).'</pre>';
		  	return $this->poll;
		}

		// Simpele generator voor grafische weergave (lengte)
		function pollCalculate($maxlengte)
		{
			$lengte = array();
			$count = $this->pollCounter();
			if ($count > 0) {
				$lengte[1] = (($this->poll['item1'] / $count) * $maxlengte);
				$lengte[2] = (($this->poll['item2'] / $count) * $maxlengte);
				$lengte[3] = (($this->poll['item3'] / $count) * $maxlengte);
			}
			return $lengte;

		}

		// Telt totaal aantal stemmen, veld 'count' wordt niet meer gebruikt!
		function pollCounter()
		{
			return ($this->poll['item1']+$this->poll['item2']+$this->poll['item3']);
		}

		// Keuze wordt verhoogt met 1, en datum wordt geupdated
		function pollUpdate($ident, $keuze)
		{
			$datum = time();
			if ($keuze == 1) {
				$count = ++$this->poll['item1'];
				$sql = "UPDATE stelling SET item1='".$count."', datum='".$datum."' WHERE id='".$ident."'";
			}
			if ($keuze == 2) {
				$count = ++$this->poll['item2'];
				$sql = "UPDATE stelling SET item2='".$count."', datum='".$datum."' WHERE id='".$ident."'";
			}
			if ($keuze == 3) {
				$count = ++$this->poll['item3'];
				$sql = "UPDATE stelling SET item3='".$count."', datum='".$datum."' WHERE id='".$ident."'";
			}
			//$handle = mysql_connect($this->dbhost, $this->dbuser, $this->dbpasswd);
    		mysql_select_db($this->dbname, $this->handle);
			$result = mysql_query($sql);
		}

		// Maak een nieuwe entry in tabel. Controle op dubbel stemmen.
		function pollUpdateVoted($ident, $user, $keuze)
		{
			$ip = $this->get_ip();
			$datum = time();
			$sql = "INSERT INTO `stelling_votes` VALUES('','".$user."','".$ident."','".$keuze."','".$datum."','".$ip."')";
			//$handle = mysql_connect($this->dbhost, $this->dbuser, $this->dbpasswd);
    		mysql_select_db($this->dbname, $this->handle);
			$result = mysql_query($sql);
		}

		// Is er al een keer gestemd? Controleer op IP en vraag.
		function pollVoted($ident)
		{
			$ip = $this->get_ip();
			$sql = "SELECT id FROM stelling_votes WHERE ip='".$ip."' AND vraag_id='".$ident."'";
			//$handle = mysql_connect($this->dbhost, $this->dbuser, $this->dbpasswd);
    		mysql_select_db($this->dbname, $this->handle);
			$result = mysql_query($sql);
			return mysql_num_rows($result);
		}

		// Haalt het IP-adres van de gebruiker op
		function get_ip()
		{
			if(getenv($_SERVER['HTTP_X_FORWARDED_FOR']))
			{
	   			$ip = getenv($_SERVER['HTTP_X_FORWARDED_FOR']);
			}
	   		elseif	(getenv($_SERVER['HTTP_CLIENT_IP']))
			{
	     		$ip = getenv($_SERVER['HTTP_CLIENT_IP']);
			}
	   		else
			{
	   			$ip = $_SERVER['REMOTE_ADDR'];
			}
			return $ip;
		}

	}

?>
[/code]
 
[pollResult.php]
[code]
<?php

	require_once('../stelling/pollClass.php');

	function results() {

		include('../db/config.php');
		$stelling = new minipoll($dbhost,$dbname,$dbuser,$dbpasswd);
		$poll = $stelling->pollGet();
		// Grafische weergave berekenen
		$lengte = $stelling->pollCalculate(130);
		// Totaal gestemd (veld 'count' in tabel niet meer nodig)
		$totaal = $stelling->pollCounter();

		?>

		<!-- Deze hoort bij de website, dit is het titelbalkje -->
		<p class="titel_menu"><?php echo stripslashes($poll['titel']); ?></p>

		<div style="margin:8px">

			<span class="pollTitel"><?php echo wrap($poll['vraag'],25); ?></span><br />

			<div style="padding-top:4px; padding-bottom:4px">

				<span class="pollTekst"><?php echo wrap($poll['antwoord1'],28); ?></span><br />
				<?php if ($lengte[1] > 0) { ?>
					<img class="pollImage" src="/img/silver.gif" width="<?php echo $lengte[1]; ?>" alt="" border="0" />&nbsp;
				<?php } ?>
				<span class="pollCounter">(<?php echo $poll['item1']; ?>)</span><br />

				<span class="pollTekst"><?php echo wrap($poll['antwoord2'],28); ?></span><br />
				<?php if ($lengte[2] > 0) { ?>
					<img class="pollImage" src="/img/silver.gif" width="<?php echo $lengte[2]; ?>" alt="" border="0" />&nbsp;
				<?php } ?>
				<span class="pollCounter">(<?php echo $poll['item2']; ?>)</span><br />

				<span class="pollTekst"><?php echo wrap($poll['antwoord3'],28); ?></span><br />
				<?php if ($lengte[3] > 0) { ?>
					<img class="pollImage" src="/img/silver.gif" width="<?php echo $lengte[3]; ?>" alt="" border="0" />&nbsp;
				<?php } ?>
				<span class="pollCounter">(<?php echo $poll['item3']; ?>)</span><br />

			</div>

			<div class="pollFooter">
				(Totaal gestemd: <strong><?php echo $totaal; ?></strong>)
			</div>

		</div>

	<?php } ?>


<?php

	//echo '<pre>'.print_r($_POST, true).'</pre>'; exit;
	//if (isset($_POST['view'])) { results(); } else {
	if (isset($_POST['view'])) { results(); } else {

		include('../db/config.php');
		$stelling = new minipoll($dbhost,$dbname,$dbuser,$dbpasswd);

		// Update keuze, datum en ip controle
		$ident = mysql_real_escape_string($_POST['ident']);
		$keuze = mysql_real_escape_string($_POST['keuze']);
		$stelling->pollUpdate($ident, $keuze);
		$stelling->pollUpdateVoted($ident, 0, $keuze);
		results();
	}
?>
[/code]

[pollStart.php]
[code]
<style type="text/css">
/*<![CDATA[*/

	/*
		Declareer hier ook voor include 'pollResults',
		anders werkt het niet goed op elke browser
	*/

	.radioButton {
		margin: 0;
		padding: 0;
		height: 15px;
	}
	.submitButton {
		font-size: 8pt;
	}
	.pollTitel {
		font-size: 9pt;
		font-weight: bold;
	}
	.pollTekst {
		font-size: 8pt;
		color: #505050;
	}
	.pollDatum {
		padding-top: 4px;
		font-size: 8pt;
	}
	.pollImage {
		padding-top:2px;
		height: 14px;
	}
	.pollCounter {
		font-size: 8pt;
		vertical-align: top;
	}
	.pollFooter {
		font-size: 8pt;
	}

/*]]>*/
</style>

<script type="text/javascript">
//<![CDATA[

	function POSTRequest(query)
	{
	   	var xmlHttp;

	   	if (window.ActiveXObject) {
	        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    else if (window.XMLHttpRequest) {
	        xmlHttp = new XMLHttpRequest();
	    }

		var queryString = "http://" + location.hostname + "/stelling/pollResults.php?" + query;

	   	xmlHttp.open("POST", queryString, true);
	   	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	   	xmlHttp.setRequestHeader("Content-length", query.length);
	   	xmlHttp.setRequestHeader("Connection", "close");
	   	xmlHttp.send(query);

	   	return xmlHttp;
	}

	function doeIets(query)
	{
		var xmlHttp = POSTRequest(query);
		xmlHttp.onreadystatechange=function()
		{
		   if(xmlHttp.readyState==4)
		      {
		      var tekst = xmlHttp.responseText;
		      document.getElementById('quickPoll').innerHTML = tekst;
		      }
		}
	}

	function stemmen()
    {
        var mijn_form = document.getElementById('poll');
        var query = 'ident=' + mijn_form.ident.value;
        for(var i = 0; i < mijn_form.keuze.length ; i++){
            if(mijn_form.keuze[i].checked) {
                query += '&keuze=' + mijn_form.keuze[i].value;
                doeIets(query);
                return true;
            }
        }
        alert('Je moet wel een keuze maken');
    }

    function resultaat()
    {
        var mijn_form = document.getElementById('poll');
        var query = 'ident=' + mijn_form.ident.value;
        query += '&view=true';
        doeIets(query);
        return true;
    }

//]]>
</script>


<?php

	require_once('../stelling/pollClass.php');

	function vote($poll) {

		?>

		<!-- Deze hoort bij de website, dit is het titelbalkje -->
		<p class="titel_menu"><?php echo stripslashes($poll['titel']); ?></p>

		<div style="margin:8px;">

			<form style="margin:0; padding:0" id="poll" action="">

				<span class="pollTitel"><?php echo wrap($poll['vraag'],25); ?></span><br />

				<div style="padding-top:4px; padding-bottom:4px">

					<table width="100%" border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td align="left" valign="top" width="20">
					    	<input class="radioButton" type="radio" id="optie1" name="keuze" value="1" />
					    </td>
						<td align="left" valign="top">
					    	<label class="pollTekst" for="optie1">
					     	 <?php echo wrap($poll['antwoord1'],26); ?>
					    	</label>
					    </td>
				    </tr>
				    <tr>
						<td align="left" valign="top" width="20">
				      		<input class="radioButton" type="radio" id="optie2" name="keuze" value="2" />
				      	</td>
				      	<td align="left" valign="top">
				    		<label class="pollTekst" for="optie2">
				      		<?php echo wrap($poll['antwoord2'],26); ?>
				    		</label>
				      	</td>
				    </tr>
					<tr>
						<td align="left" valign="top" width="20">
				      		<input class="radioButton" type="radio" id="optie3" name="keuze" value="3" />
				      	</td>
				      	<td align="left" valign="top">
				    		<label class="pollTekst" for="optie3">
				      		<?php echo wrap($poll['antwoord3'],26); ?>
				    		</label>
				      	</td>
				    </tr>
				    </table>

			    </div>

		   		<input type="hidden" name="ident" value="<?php echo $poll['id']; ?>" />

			</form>

			<!-- Moet buiten form staan anders werkt het niet! -->
			<input class="submitButton" type="submit" value="Stemmen&nbsp;" onclick="javascript:stemmen()" />
			<input class="submitButton" type="submit" value="Bekijken&nbsp;" onclick="javascript:resultaat()" />

			<div class="pollDatum">
				(<?php echo strftime("%d %B %Y om %H:%M", $poll['datum']); ?>)
			</div>

		</div>

	<?php } ?>


<div id="quickPoll"> <?php

		include('../db/config.php');
		$stelling = new minipoll($dbhost,$dbname,$dbuser,$dbpasswd);
		// Indien poll actief?
		if ($stelling->count > 0) {
			$poll = $stelling->pollGet();
			// Uitzoeken welke weergave
			if ($stelling->pollVoted($stelling->ident) == 0) { vote($poll); }
				else { ?>
					<script language="JavaScript">
						doeIets('view=true');
					</script>
				<?php }
		}	?>
</div>
[/code]
