Poll Class
Posted: Sat Jun 04, 2005 7:59 pm
Hi,
I'm having difficulties with the following Poll class:
Obviosuly some of the functions havn't been finished yet. As it is now I'd expect it to be able to get results from the database and display them.
Now I know that connections to the database and the SQL statements are working correctly because some of them are used elsewhere on the site.
At the moment neither the question or the options are displayed as a result of:
All I get is the empty markup. Any ideas?
I'm having difficulties with the following Poll class:
Code: Select all
// Configuration
require_once("common/pollConfig.php");
class Poll {
<?php
// Member variables
var $options;
var $question;
// Initializes the poll
function Poll($id) {
$this->connect();
$sql = "SELECT * FROM poll_main, poll_option
WHERE poll_main.pollID = '" . $id . "'
AND poll_main.pollID = poll_option.pollID
LIMIT " . MAX_Q;
$result = mysql_query($sql);
echo mysql_error();
// Resolve result into member vars
$this->options = array();
while($row = mysql_fetch_array($result)) {
$this->options[] = $row["option"];
if(!isset($this->question))
$this->question = $row["question"];
}
mysql_close();
}
// Connect to database and retrieve information
function connect() {
$this->mysqlConnection = mysql_connect($dbHost, $dbUser, $dbPword);
$this->mysqlDatabase = mysql_select_db($db);
}
// Called to display the markup for the vote
function display() {
GLOBAL $PHP_SELF;
if(!isset($_COOKIE["hasVoted"])) {
echo "\t\t<div id=\"poll\">
<h2 id=\"h2poll\"><span class=\"hide\">Poll</span></h2>
<p>" . $this->question . "</p>
<form action=\"" . $PHP_SELF . (isset($_GET["id"]) ? "?id=" . $_GET["id"] : "" ) . "\" method=\"post\">
<input type=\"hidden\" id=\"action\" value=\"postVote\" />\n";
// Iterate through the results, displaying them as options
for($i = 0; $i < count($this->options); $i++) {
echo"\t\t\t\t<p><input type=\"radio\" name=\"choice\" value=\"" . $this->options[$i]
. "\" " . ($i == 0 ? "checked=\"checked\"" : "" ) . " />" . $this->options[$i]
. "</p>\n";
}
echo "\t\t\t\t<p class=\"submit\"><input type=\"submit\" id=\"submit\" value=\"Vote\" /></p>\n";
echo "\t\t\t</form>\n\t\t</div>";
}
else {
// Display the graph of the current votes
echo "<p>You have already voted! This part of the script isn't quite finished yet!</p>";
}
}
function vote($option) {
// Set voted cookie
setcookie("hasVoted", true);
// Update database
}
}
// Updates the poll if the form has been sent.
function hasVoted($poll) {
if(isset($_POST["action"]) && $_POST["action"] == "postVote")
$poll->vote($_POST["choice"]);
}
?>Now I know that connections to the database and the SQL statements are working correctly because some of them are used elsewhere on the site.
At the moment neither the question or the options are displayed as a result of:
Code: Select all
$Poll->display();