Ik heb een map maaw een poll in staat. Dit zijn de bestanden index.php, poll.php en database.php. Alleen als ik index.php in mijn indexpagina include, krijg ik de volgende foutmeldingen:
Notice: Undefined index: vote in /home/ritshitn/domains/ritshit.nl/public_html/poll/index.php on line 32
Notice: Undefined variable: poll in /home/ritshitn/domains/ritshit.nl/public_html/poll/poll.php on line 21
Dit zijn de scripts:
index.php:
<?
# Filename : index.php
# Author : Mitch Vroege
# Projectname : SMART PoLL
# Projectversion : 0.7.1
# Releasedate : 16-08-2004
# 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();
}
}
}
?>
poll:
<?
# Filename : poll.php
# Author : Ritshit.nl
# Projectname : Poll
# Projectversion : 0.7.1
# Releasedate : 02-10-2005
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 /><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 .= "</p>";
$poll .= "<input type=\"submit\" class=\"loginbutton\" name=\"vote\" value=\" \" />";
$poll .= "</form>";
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 .= "<table cellpadding=\"0\" cellspacing=\"0\"><tr>
<td valign=\"top\" align=\"left\" height=\"20\" valign=\"top\">" . $data['poll_stelling'] . "</td></tr></table>";
# Loop through results
for($i = 0;$i <= $option; $i++)
{
$results .= $options[$i] . "<br />";
$results .= "
<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td width=\"3\">
<img src=\"/images/poll/left1.gif\" width=\"3\" height=\"10\" /></td> <td><img src=\"/images/poll/middle.PNG\" width=\"" . $this->imgPXL($votes, $scores[$i]) . "\" height=\"10\" /></td><td align=\"left\"><img src=\"/images/poll/right.gif\" width=\"3\" height=\"10\" /></td>
</tr>
</table> ". $this->imgPXL($votes, $scores[$i]) . "%<br>";
}
$results .= "";
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] ;
$results .= "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td width=\"3\">
<img src=\"/images/poll/left1.gif\" width=\"3\" height=\"10\" /></td> <td><img src=\"/images/poll/middle.PNG\" width=\"" . $this->imgPXL($votes, $scores[$i]) . "\" height=\"10\" /></td><td align=\"left\"><img src=\"/images/poll/right.gif\" width=\"3\" height=\"10\" /></td>
</tr>
</table>" . $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=\"/admin/?id=poll/archives\">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;
}
}
?>
Database.php:
<?
# Filename : database.php
# Author : Mitch Vroege
# Projectname : SMART PoLL
# Projectversion : 0.7.1
# Releasedate : 16-08-2004
class database
{
# Database vars
var $user = "****";
var $pass = "*****";
var $host = "***";
var $name = "****";
# Functie die mysql_num_rows teruggeeft
function num_rows($sql)
{
$this->connect();
$res = mysql_query($sql);
$aantal = mysql_num_rows($res);
mysql_close();
return $aantal;
}
# Functie de mysql_fetch_array teruggeeft
function get_array($sql)
{
$this->connect();
$res = mysql_query($sql);
while($data = mysql_fetch_array($res))
$row[] = $data;
mysql_close();
return $row;
}
# Zelfde als hierboven maar dan 1 row
function get_single($sql)
{
$this->connect();
$res = mysql_query($sql);
$data = mysql_fetch_array($res);
mysql_close();
return $data;
}
# Functie die een normale query runt
function execute($sql)
{
$this->connect();
$res = mysql_query($sql);
mysql_close();
return $res;
}
# Functie die de database connectie maakt
function connect()
{
return mysql_select_db($this->name, mysql_connect($this->host, $this->user, $this->pass));
}
}
?>
667 views