On 23-06-2010 23:17, Tommy K wrote:
> Bertel Lund Hansen wrote in
> dk.edb.internet.webdesign.serverside.php:
>> Tommy K skrev:
>>
>>> Jeg leder efter en poll løsning til min kommende hjemmeside,
>>
>> Hvad skal den poll-løsning kunne?
>>
>> --
>> Bertel
>>
http://bertel.lundhansen.dk/ FIDUSO:
http://fiduso.dk/
> Jeg skal bare kunne udfylde spørgsmålet, redigere i antallet af
> svarmuligheder(og teksten) og så skal den vise svarene i procent
> når man har svaret.
Lav en .txt fil - så behøver du ikke lave en administration og alt
muligt til det :)
-- poll.txt --
Q: Hvilket nummer er størst?
A1: 1
AA1: 0
A2: 2
AA2: 0
A2: 3
AA2: 0
-- php.fil --
<?php
class FilePoll
{
private $filename;
public $template = '<h3>__Q__</h3><ul>__A__</ul>';
public function __construct($filename)
{
if (! file_exists($filename)) {
trigger_error('Filename \'' . $filename . '\' does not exists');
}
$this->filename = $filename;
}
private function loadPoll()
{
$lines = explode("\n", trim(file_get_contents($this->filename)));
foreach($lines AS $line) {
list($type, $content) = explode(':', $line);
switch($type) {
case 'Q': $question = $content;
case 'A': $answers[(int) str_replace('A', '', $type)]['options'] =
$content;
case 'AA': $answers[(int) str_replace('AA', '',
$type)]['numanswers'] = $content;
}
}
return array('question' => $question, 'answers' => $answers);
}
private function
private function checkAnswer($answer)
{
$options = $this->loadPoll();
if (! isset($options['answers'][$answer])) return false;
}
public function vote($answer)
{
$answer = (int) $answer;
if ($this->checkAnswer($answer)) {
$lines = explode("\n", trim(file_get_contents($this->filename)));
foreach($lines AS $i => $line) {
list($type, $content) = explode(':', $line);
if ($type != 'AA' . $answer) continue;
else {
$content = (int) $content;
$lines[$i] = 'AA' . $answer . ': ' . $content++;
break;
}
}
file_put_contents($this->filename, implode("\n", $lines));
}
}
public function drawPoll()
{
$options = $this->loadPoll();
$out = '';
foreach($options['answers'] AS $id => $content) {
$out .= '<li><a href="' . $_SERVER['PHP_SELF'] . '?answer=' . $id .
'">' . $content . '</a></li>';
}
return str_replace('__Q__', $options['question'], str_replace('__A__',
$out, $this->template));
}
}
$filename = 'poll.txt';
$poll = new FilePoll($filename);
if (isset($_GET['answer'])) {
$poll->vote($_GET['answer']);
}
echo $poll->drawPoll();