Page 1 of 1

How to define a results page

Posted: Sat Feb 17, 2007 12:33 pm
by Gregor
Hello,

I'm stucked and could use some help on how to define the result page for a poll system (Simpoll). The red line in the code, asks a page where I want to display the results. Lets assume the page in the menu is called "enquete_results"

Code: Select all

<?php
class miniPoll {

	var $show_vote_count;
	var $active_poll_id;
	var $active_poll_title;
	var $timestamp;
	var $timeout;
	var $ip;
	var $repeated_vote;
	var $results_page;
	var $old_polls;

	function miniPoll() {
		$this->show_vote_count = true; // display total votes? true/false
		$this->pollLayout();
		$this->getActivePoll();
		$this->timestamp = time();
		$this->timeout = $this->timestamp - 1800;
		$this->ip = $_SERVER['REMOTE_ADDR'];
		$this->repeated_vote = "You already voted today<br />";
		[color=red]$this->results_page = "test_poll_results.php"; // page where you display results[/color]
		$this->old_polls = true; // if true enables view of old polls. this only display old polls it doesn't allow users to vote. true/false
		
	}
	
	function pollLayout() {
		// it allows you to set visual settings using CSS definitions included in file where you're calling this class
		// replace these with your own CSS styles
		$this->form_table_width = "120px";
		$this->form_title = "menuhd";
		$this->form_table = "tabele";
		$this->form_table_cell = "poll";
		$this->form_button = "formlook";
		$this->poll_question = "fat"; // this is for <span> tag
		$this->results_title = "menuhd";
		$this->results_table = "";
		$this->results_poll_question = "fat";
		$this->result_table_width = "450px";
		$this->result_table_cell = "pollbg";
		$this->bar_image = "images/bar.jpg"; // please select 1px width x 15px height image
	}
	
	function getActivePoll() {
		$sql = @mysql_query ("SELECT pollid, polltitle FROM poll_desc WHERE status = 'active'");
		$row = @mysql_fetch_object($sql);
		$this->active_poll_id = $row->pollid;
		$this->active_poll_title = $row->polltitle;
		return;
	}
	
	function voteCount() {
		$sql = @mysql_query ("SELECT SUM(votecount) AS votecount FROM poll_data WHERE pollid = '$this->active_poll_id'");
		$row = @mysql_fetch_object($sql);
		$this->votecount = $row->votecount;
		return $this->votecount;
	}
	
	function pollForm() {
		$sql = @mysql_query ("SELECT polltext, voteid FROM poll_data WHERE pollid = '$this->active_poll_id' ORDER BY voteid");
		if (@mysql_num_rows($sql) > 0) {
			echo "<table width=\"$this->form_table_width\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" class=\"$this->form_table\">
			<tr><td class=\"$this->form_title\">Mini Poll</td></tr>
			<tr><td class=\"$this->form_table_cell\">\r\n";
			echo "<form action=\"$this->results_page\" name=\"pollf\" id=\"pollf\" method=\"get\">
			<span class=\"$this->poll_question\">" . $this->active_poll_title . "</span><br />\r\n";
			
			while ($row = @mysql_fetch_object($sql)) {
				if (!empty($row->polltext)) {
					echo "\t<input type=\"radio\" name=\"voteid\" value=\"$row->voteid\" /> $row->polltext <br />\r\n";
				}
			}
			
			echo "<input type=\"hidden\" name=\"pollid\" id=\"pollid\" value=\"$this->active_poll_id\" /><br />\r\n";
			echo "<input type=\"submit\" name=\"poll\" id=\"poll\" value=\"Vote\" class=\"$this->form_button\" />
			<hr size=\"1\" noshade=\"noshade\" />";
			if ($this->show_vote_count) {
				echo "Total votes: " . $this->voteCount() . "\r\n";
			}
			echo "<a href=\"$this->results_page?pollid=$this->active_poll_id\">View results</a>
			</form>\r\n</td></tr></table>\r\n";
		}
	}
	
	function deleteCheck() {
		$sql = @mysql_query ("DELETE FROM poll_check WHERE time < '$this->timeout'");
		return;
	}
	
	function insertCheck() {
		$sql = @mysql_query ("INSERT INTO poll_check (ip, time) VALUES ('$this->ip', '$this->timestamp')");
		return;
	}
	
	function voteCheck() {
		$this->deleteCheck();
		$sql = @mysql_query ("SELECT ip FROM poll_check WHERE ip = '$this->ip'");
		if (@mysql_num_rows($sql) == 0) {
			$this->insertCheck();
			return true;
		}
		else {
			return false;
		}
	}
	
	function processPoll($pollid, $voteid) {
		if ($this->voteCheck()) {
			$sql = @mysql_query ("UPDATE poll_data SET votecount = votecount + 1 WHERE voteid = '$voteid' AND pollid = '$pollid'");
		}
		else {
			echo $this->repeated_vote;
		}
	
	}
	
	function selectedPoll($pollid) {
		$sql = @mysql_query ("SELECT polltitle FROM poll_desc WHERE pollid = '$pollid'");
		$row = @mysql_fetch_object($sql);
		$this->polltitle = $row->polltitle;
		return $this->polltitle;
	}
	
	function selectedPollVotecount($pollid) {
		$sql = @mysql_query ("SELECT SUM(votecount) AS votecount FROM poll_data WHERE pollid = '$pollid'");
		$row = @mysql_fetch_object($sql);
		$this->votecount = $row->votecount;
		return $this->votecount;
	}
	
	function formatDate($val) {
		$arr = explode("-", $val);
		return date("d. F Y.", mktime (0,0,0, $arr[1], $arr[2], $arr[0]));
	}
	
	function oldPolls($pollid) {
		$sql = mysql_query ("SELECT pollid, polltitle, timestamp FROM poll_desc WHERE pollid <> '$pollid'");
		if (mysql_num_rows($sql) > 0) {
			echo "<tr><td class=\"$this->result_table_cell\" colspan=\"2\">\r\n";
			while ($row = mysql_fetch_object($sql)) {
				$datum = $this->formatDate($row->timestamp);
				echo "<a href=\"$this->results_page?pollid=$row->pollid\">$row->polltitle</a> $datum<br />\r\n";
			}
			echo "</td></tr>\r\n";
		}
	}
	
	function pollResults($pollid) {
		$this->selectedPoll($pollid);
		$this->selectedPollVotecount($pollid);
		$sql = @mysql_query ("SELECT polltext, votecount, voteid FROM poll_data WHERE pollid = '$pollid' AND polltext <> '' ORDER BY voteid");
		echo "<table border=\"0\" width=\"$this->result_table_width\" class=\"$this->results_table\">
		<tr><td class=\"$this->results_title\" colspan=\"2\">Mini Poll Results</td></tr>";
		if (@mysql_num_rows($sql) > 0) {
			echo "<tr><td class=\"$this->results_poll_question\" colspan=\"2\">$this->polltitle</td></tr>\r\n";
			while ($row = mysql_fetch_object($sql)) {
				if ($this->votecount == 0) {
					$tmp_votecount = 1;
				}
				else {
					$tmp_votecount = $this->votecount;
				}
				$vote_percents = number_format(($row->votecount / $tmp_votecount * 100), 2);
				$image_width = intval($vote_percents * 3);
				echo "<tr><td class=\"$this->result_table_cell\">$row->polltext $row->votecount votes. ($vote_percents %)</td><td class=\"$this->result_table_cell\"> <img src=\"$this->bar_image\" width=\"$image_width\" alt=\"$vote_percents %\" height=\"15\" /> </td></tr>\r\n";
			}
			echo "<tr><td class=\"$this->result_table_cell\" colspan=\"2\">Total votes: $this->votecount</td></tr>\r\n";
		}
		if ($this->old_polls) {
			$this->oldPolls($pollid);
		}
		echo "</table>\r\n";
		// if you like this software and you find it useful, please don't remove this link
		echo "Powered by: <a href=\"http://www.sim-php.info\">SimPoll</a> v.1.0\r\n";
	
	}

}
?>
Thanks for your help,
G

Re: How to define a results page

Posted: Sat Mar 03, 2007 10:07 am
by Gregor
Hello,

Could someone please help me?

Thanks in advance,
G

Re: How to define a results page

Posted: Sat Mar 03, 2007 12:20 pm
by Pierre M.
Hello Gregor,

I can't help you much, as I'm in the "no code" camp (I prefer to use out of the box CMSms with modules).
Dou you know http://dev.cmsmadesimple.org/projects/ppolls ? Have you tried this poll module ?
If it is unmaintained or underpowered compared to your polling solution, may be you can work with the module maker to get the integration your aiming at ? Both of you would get improved software...
Have fun hacking !

Pierre M.

Re: How to define a results page

Posted: Sat Mar 03, 2007 1:18 pm
by Gregor
Thanks Pierre M.

Thanks for your reaction. I tried the out of the box modules, however it does not satisfy my requirements. Like you, I'm in the 'no code'-camp. Hope someone else might be able to help me in the right direction.

G

Re: How to define a results page

Posted: Sat Mar 03, 2007 2:57 pm
by RonnyK
Gregor,

often is the result-page nothing more than a page that is opened after sending the mail/poll etc.., stating something like "thanks for blabla, we'll contact as soon as possible, or whatever text". So you can try to add a content-page that is active but not shown in menu. If you enter the link starting with "index.php?page=****", it might work. Otherwise you might just save a static page and link to that, but I think that the link must work.

Ronny

Re: How to define a results page

Posted: Sat Mar 03, 2007 4:04 pm
by Gregor
Hi Ronny,

If I enter code like:

Code: Select all

$this->index.php?page=enquete_results_2 = "test_poll_results.php"; // results_page page where you display results
than this error comes up
Parse error: syntax error, unexpected '=' in /home/wsvherking/domains/wsv-herkingen.nl/public_html/includes/miniPoll.class.php on line 22
If I make it

Code: Select all

$this->enquete_results_2 = "test_poll_results.php"; // results_page page where you display results
than it gets me to the home page without displaying any results. This is where I got stucked.

Do have another suggestion?

Thanks,
Gregor

Re: How to define a results page

Posted: Sat Mar 03, 2007 4:19 pm
by RonnyK
Gregor,

I don't know how to solve the issue with the '=' sign as it is part of the pages in CMS. You might try creating a page, that is stored outside CMSMS, and point to that page. That way you can go around the '='-issue. What if you just create a simple page and put it in the root of your site and point directly there. if it works, you can expand the logic. Maybe others have better ideas of solving this inside CMSMS.

Ronny

Re: How to define a results page

Posted: Sat Mar 03, 2007 5:38 pm
by chead
From a quick look at the Simpoll documentation, there are several things you'll probably have to do:
  • Create user-defined tags for the different code blocks they show at the link above for steps 3-5. Give them names like simpoll_show_poll, simpoll_display_results, simpoll_admin Make sure the "include_once" path matches where you've saved the Simpoll PHP files
  • Create a CMSMS page to view results; give it a page id like "test_poll_results"
  • Set the "results page" in the Simpoll PHP file to point to the CMSMS results page:
    $this->results_page = "index.php?page=test_poll_results"; // results_page page where you display results
  • In the content area of the CMSMS results page, add the user-defined tag for displaying results: {simpoll_display_results}
  • Set the "admin page" to point to another CMSMS page for poll administration, put the {simpoll_admin tag} on that page, and define the result page in the admin file to $this->results_page = "index.php?page=test_poll_admin";
Try that; it should get you heading in the right direction.

Re: How to define a results page

Posted: Mon Mar 05, 2007 6:19 am
by Gregor
Thanks for all your help. I set it up like you suggested. So far no luck.

The poll-page shows up correct. After entering a poll, I'm back at the home page and the url says:
The results page remains empty, so prbably no poll results are saved:
Have you got any other sugestion?

I disabled the menu options (it is a production environment...)

Thanks,
Gregor

Re: How to define a results page

Posted: Mon Mar 05, 2007 1:50 pm
by chead
Looks like there will be more to it. After downloading Simpoll, I see that there are other files besides the class file.

It will take some code rewriting to because the way it's set up now, it uses parameters like voteid=1&pollid=1&poll=Vote that CMSMS doesn't understand. That's why the first link just takes you back to the home page.

Also, the admin, results and display page code would need to be stripped down. Right now they output an entire HTML page, so even if it worked, you would have problems with duplicated HTML, BODY and other tags. You can see this if you look at the HTML source in your second link.

The second part isn't too hard; the first one is outside my expertise. Sorry that I can't help more. You might change the thread topic to something about a poll to get more attention for it.

If you get it working, be sure to let us know how you did it!

Re: How to define a results page

Posted: Mon Mar 05, 2007 2:21 pm
by Gregor
Thanks Chead for you research. I'm not in to php-writing, however I'll post this topic into as a new thread.