Poll Class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
MathewByrne
Forum Commoner
Posts: 38
Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia

Poll Class

Post by MathewByrne »

Hi,

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"]);
}

?>
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:

Code: Select all

$Poll->display();
All I get is the empty markup. Any ideas?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Code: Select all

$example = new Poll;
$example->display();
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

You probably already did this but, I noticed on your post that <?php tag comes after class poll {. Also did you instantiate the class?

Code: Select all

$poll = new poll;
$poll->display();
Does anything echo out with display()?

In the display method is the cookie set?

Code: Select all

!isset($_COOKIE["hasVoted"]
I also noticed that the following variables are not defined within in the scope of the script -- $dbhost, $dbUser, $dbPword

Code: Select all

$this->mysqlConnection = mysql_connect($dbHost, $dbUser, $dbPword);
If they aren't defined in the class they won't work. You can make them global from the config file if that's where they are coming from like this:

Code: Select all

function connect() {
global $dbHost, $dbUser, $dbPword, $db;
        $this->mysqlConnection = mysql_connect($dbHost, $dbUser, $dbPword);
        $this->mysqlDatabase = mysql_select_db($db); 
    }
Hope this helps...
User avatar
MathewByrne
Forum Commoner
Posts: 38
Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia

Post by MathewByrne »

Ok, thanks for the quick reply.

Yes I have done the following from where the class is being used:

Code: Select all

$poll = new Poll($id);
...
$poll->display();
Also the <?php where it was was a typo :oops: and the Cookie is incomplete funtionality atm.

I've also changed the connect() function to the following:

Code: Select all

// Connect to database and retrieve information
function connect() {
	global $dbHost, $dbUser, $dbPword, $db;

	$this->mysqlConnection = mysql_connect($dbHost, $dbUser, $dbPword);
	$this->mysqlDatabase = mysql_select_db($db); 
}
But no result. One other thing that I tried was putting the following line at the end of the Constructor:

Code: Select all

$this->question = "test";
But test does not get printed out when display() is called. It seems almost as though the constructor isn't being called at all. Is there an easier way to debug this? I'm currently just uploading onto my server and testing it through a browser. The servar is using php 4.3.10.
Thanks
User avatar
MathewByrne
Forum Commoner
Posts: 38
Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia

Post by MathewByrne »

Oh, and yeah the markup displays when display() is called. Something like this:

Code: Select all

<div id=&quote;poll&quote;>
	<h2 id=&quote;h2poll&quote;><span class=&quote;hide&quote;>Poll</span></h2>
	<p></p>
	<form action=&quote;/temp/index.php&quote; method=&quote;post&quote;>
		<input type=&quote;hidden&quote; id=&quote;action&quote; value=&quote;postVote&quote; />
		<p class=&quote;submit&quote;><input type=&quote;submit&quote; id=&quote;submit&quote; value=&quote;Vote&quote; /></p>
	</form>
</div>
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

First settle the question of whether data is being pulled or not...

Change the constructor function.

Code: Select all

while($row = mysql_fetch_array($result)) {
            $this->options[] = $row["option"];
            if(!isset($this->question))
                $this->question = $row["question"];
        }

var_dump($this->options);
var_dump($this->question);
Var_dump() the arrays from the database and see if you are getting data into the arrays.
User avatar
MathewByrne
Forum Commoner
Posts: 38
Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia

Post by MathewByrne »

Working now, the problem was not in the Class but in the way it was being called! The main file was loading old Poll variables from previous sessions when the class itself had been updated. Thanks.
Post Reply