Oh I see, thanks for pointing out that.
Changed the path now to
C:\Program Files\Apache Group\Apache2\htdocs\poll\current.txt
Although the fopen() echo test yields a "SUCCESS", nothing shows up on the script. One interesting thing I noted is that when loading the page in IE, the whole page except the poll shows up, and the progress bar at the bottom shows that the page is still loading. Tried waiting for a few minutes but there was little progress. That doesn't happen for the fopen() test however.
Here's the code hexpoll.php
<?php
class HexPoll {
//CLASS MEMBERS
private $pollFile; //Source of poll file
private $questionTemplate; //Template to show question
private $optionTemplate; //Template to show option
private $resultTemplate; //Template to show results
private $resultOptionTemplate; //Template to show results options
private $graphMaxLength; //Maximum length of results graph in pixels
private $qoSeparator; //Question-Options delimiter
private $ovSeparator; //Option-Votes delimiter
//RESTRICTED CLASS MEMBERS
private $question;
private $options = array();
private $votes = array();
private $percent = array();
//GETTER METHODS
public function getPollFile() {
return $this->pollFile;
}
public function getQuestionTemplate() {
return $this->questionTemplate;
}
public function getOptionTemplate() {
return $this->optionTemplate;
}
public function getResultTemplate() {
return $this->resultTemplate;
}
public function getGraphMaxLength() {
return $this->graphMaxLength;
}
public function getQOSeparator() {
return $this->qoSeparator;
}
public function getOVSeparator() {
return $this->ovSeparator;
}
//SETTER METHODS
public function setPollFile($pollFile) {
$this->pollFile = $pollFile;
}
public function setQuestionTemplate($questionTemplate) {
$this->QuestionTemplate = $questionTemplate;
}
public function setOptionTemplate($optionTemplate) {
$this->QuestionTemplate = $optionTemplate;
}
public function setResultTemplate($resultTemplate) {
$this->QuestionTemplate = $resultTemplate;
}
public function setResultOptionTemplate($resultOptionTemplate) {
$this->resultOptionTemplate = $resultOptionTemplate;
}
public function setGraphMaxLength($graphMaxLength) {
$this->graphMaxLength = $graphMaxLength;
}
public function setQOSeparator($qoSeparator) {
$this->qoSeparator = $qoSeparator;
}
public function setOVSeparator($ovSeparator) {
$this->ovSeparator = $ovSeparator;
}
//FRONT-END METHODS
public function showQuestion() {
$this->loadPoll();
$result = str_replace('[QUESTION]',$this->question, $this->questionTemplate);
print $result;
}
public function showOption($optionNo) {
$this->loadPoll();
$result = str_replace('[OPTION]', $this->option[$optionNo], $this->optionTemplate);
print $result;
}
public function showPoll() {
$this->loadPoll();
$this->showQuestion();
for ($i = 0; $i < count($this->options); $i++) {
$this->showOption($i);
}
}
public function showPollResults($pollName, $submitCheck) {
$this->loadPoll();
if($_POST[$submitCheck] == 'yes') {
$main = '';
$str1 = '';
$str2 = '';
$temp = 0;
$filer = fopen($this->pollFile,'r') or die ('Unable to open file.');
while (!feof($filer)) {
$s = fgets($filer);
if (strstr($s, $this->qoSeparator)) {
$str1 = $str1 . $s;
}
elseif (strstr($s, $_POST[$pollName]) && $temp == 0) {
$index = strpos($s, $this->ovSeparator);
$temp = (integer) substr($s, $index + 1);
$temp = (integer) $temp + 1;
$main = substr($s, 0, $index) . $this->ovSeparator . $temp . "\n";
$temp = 1;
}
elseif ($temp == 0) {
$str1 .= $s;
}
else {
$str2 .= $s;
}
}
fclose($filer);
$filew = fopen($this->pollFile,'w') or die ('Unable to open file.');
fwrite($filew, $str1 . $main . $str2);
fclose($filew);
}
$result = str_replace('[QUESTION]', $this->question, $this->resultTemplate);
print (substr($result, 0, (strpos($result, '[RESULTS]'))));
for ($i = 0; $i < count($this->options); $i++) {
$len = round($this->percent[$i] * $this->graphMaxLength);
$result2 = str_replace('[OPTION]', $this->option[$i], $this->resultOptionTemplate);
$result2 = str_replace('[OPTIONVOTES]', $this->votes[$i], $result2);
$result2 = str_replace('[GRAPHLENGTH]', $len, $result2);
$result2 = str_replace('[OPTIONPERCENT]', $this->percent[$i], $result2);
print $result2;
}
print (substr($result, 0, (strpos($result, '[RESULTS]') + 9)));
}
//BACK-END METHODS
private function loadPoll() {
$temp = 0;
$totalo = 0;
$totalv = 0;
$filer = fopen($this->pollFile,'r') or die ('Unable to open file.');
while (!feof($filer)) {
if ($temp == 0) {
$this->question = fgets($filer);
$temp = 1;
}
else {
$s = fgets($filer);
if (strstr($s,$this->ovSeparator)) {
$index = strpos($s, $this->ovSeparator);
$this->options[$totalo] = substr($s, 0, $index);
$this->votes[$totalo] = (integer) substr($s, $index+1);
$totalo++;
$totalv += (integer) substr($s, $index+1);
}
}
}
fclose($filer);
for ($i = 0; $i < $totalo; $i++) {
$this->percent[$i] = round($this->votes[$i] / $totalv * 150);
}
}
}
?>
and the code for showing the poll:
<?php
require_once('hexpoll.php');
$questionTemplate = "<div style=\"text-align:center;margin-left:10px;margin-right:10px;\">[QUESTION]</div>";
$optionTemplate = "<ins style=\"margin-left:10px;\"><input type=\"radio\" name=\"vote\" value=\"[OPTION]\" />[OPTION]</ins>\n";
$poll = new HexPoll();
$poll->setPollFile('C:\Program Files\Apache Group\Apache2\htdocs\poll\current.txt');
$poll->setQuestionTemplate($questionTemplate);
$poll->setOptionTemplate($optionTemplate);
$poll->setQOSeparator('/');
$poll->setOVSeparator('|');
$poll->showPoll();
?>
and the code to show the results:
<?php
require_once('hexpoll.php');
$resultTemplate = <<<ENDOF
<div class="bold">[QUESTION]</div>
<ins><hr style="color:#CCCCCC;width:300px;position:absolute;left:0px;top:30px;" /></ins>
<ins><br /></ins>
<ins><br /></ins>
<ins><br /></ins>
<table width="250">
<tr>
<td colspan"2"></td>
</tr>
[RESULTS]
<tr>
<td colspan='2'>
<ins><hr style="color:#CCCCCC;width:300px;position:absolute;left:0px;" /></ins>
<ins><br /></ins>
<ins<br /></ins>
<div style="text-align:center;"><ins id="closeWin" style="color:#FFFFFF" onmouseover="changeStyle(1,'closeWin')" onmouseout="changeStyle(0,'closeWin')" onclick="window.close()">Close Window</ins></div>
</td>
</tr>
</table>
ENDOF;
$resultOptionTemplate = <<<ENDOF2
<tr>
<td width='90'>[OPTION]</td>
<td><ins><img src='http://127.0.0.1/libs/hexpoll/bar.gif' height='10' width='[GRAPHLENGTH]' alt='' /></ins>[OPTIONVOTES]</td>
</tr>
ENDOF2;
$poll = new HEXPoll();
$poll->setPollFile('C:\Program Files\Apache Group\Apache2\htdocs\poll\current.txt');
$poll->setResultTemplate($resultTemplate);
$poll->setResultOptionTemplate($resultOptionTemplate);
$poll->setGraphMaxLength(150);
$poll->setQOSeparator('/');
$poll->setOVSeparator('|');
$poll->showPollResults('vote','hasvoted');
?>
'vote' is the value of the name attribute in the radio buttons,
'hasvoted' is the value of the name attribute of the hidden form field to check if the user has voted
And in case you're wondering, the poll file format is something like this:
"Is 2005 a memorable year for you?"
/
Yes|28
No|14