1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with Adding Poll to a Website

Discussion in 'Content Management' started by chris20492002, Jun 2, 2006.

  1. #1
    Ok I am a Total Newbie and I have gone with the hardest Template to work with to begin with.

    I bought The .PSD Template.

    First up I wanted to get the Poll out of the way.

    How do I integrate a Poll into the Website??

    What I want is it to show the options and once the user has chosen a option it shows the results but it tracks cookies and ip addresses so that the same user cannot vote twice.

    Anyone who can Help please help.
     
    chris20492002, Jun 2, 2006 IP
  2. brian394

    brian394 Well-Known Member

    Messages:
    226
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Polls require a server-side scripting language to process the results. Because the results of the poll have to be saved somehwere, they are saved on the server, and if you want to write to a file on the server you need a server-side scripting language. I could post an example for you but you have to tell me what language your web server supports (ASP, PHP, CGI, etc.) so I know what language to write it in. Or you can just tell me who your web hosting company is and I can find out on their site.
     
    brian394, Jun 2, 2006 IP
  3. chris20492002

    chris20492002 Guest

    Messages:
    1,136
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #3

    www.estyles.com.au

    I wouldnt have a clue as to what lanugage it accepts.
     
    chris20492002, Jun 2, 2006 IP
  4. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #4
    
    Response Headers - http://www.estyles.com.au/
    
    Date: Sat, 03 Jun 2006 06:15:53 GMT
    Server: Apache/1.3.36 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 FrontPage/5.0.2.2635.SR1.2 mod_ssl/2.8.27 OpenSSL/0.9.7a PHP-CGI/0.1b
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Pragma: no-cache
    X-Powered-By: PHP/4.4.1
    Keep-Alive: timeout=15, max=100
    Connection: Keep-Alive
    Transfer-Encoding: chunked
    Content-Type: text/html
    
    200 OK
    Code (markup):
    PHP, as a CGI? How odd on an Apache server.

    cheers,

    gary
     
    kk5st, Jun 2, 2006 IP
  5. chris20492002

    chris20492002 Guest

    Messages:
    1,136
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #5

    Hi did ya happen to get a example running? Thanks in Advance.
     
    chris20492002, Jun 3, 2006 IP
  6. rewlie

    rewlie Active Member

    Messages:
    937
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    58
    #6
    you are totally newbie in means, and you make it harder by buying that PSD template, is there no ready-made one ?? i suggest you learn about cms, you are suitable for wysiwyg mode, not made-from-scratch guy.
     
    rewlie, Jun 6, 2006 IP
  7. chris20492002

    chris20492002 Guest

    Messages:
    1,136
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #7

    im on a budget I thought I could learn as I go along.

    Ive got simple links ect. undercontrol so far thats about it.

    See I can download polls but I want it to blend into my website and I duno how to do that.

    If I cant do something I will pay someone later on if I have to.
     
    chris20492002, Jun 7, 2006 IP
  8. chris20492002

    chris20492002 Guest

    Messages:
    1,136
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #8
    BTW reading up on CMS now Thanks just learnt it was

    Content management system.
     
    chris20492002, Jun 7, 2006 IP
  9. brian394

    brian394 Well-Known Member

    Messages:
    226
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    108
    #9
    Hey chris, sorry it took me so long, here's a little poll I whipped up for you that should get you started...

    Demo: http://www.ppcalc.com/test_code/poll/poll.php

    In this example you only have to create one small text file called "poll_questions.txt" to keep all of your poll questions and answers in. They must be in this format..

    [Question] [TAB] [Answer 1] [TAB] [Answer 2] [TAB] [Answer 3] etc..

    With one question on each line. You can have an unlimited number of answers for each question. You can download a sample "poll questions" text file I made here.

    Ok, now that you've done that simply copy and paste this code into a new file called "poll.php"

    poll.php
    
    <?php
    
    $questions_file_location = './poll_questions.txt';
    $responses_file_location = './poll_responses.txt';
    $poll_ips_file_location = './poll_ips.txt';
    
    /* *************************************************************************************************** */
    
    /* file_puts_contents() function from PEAR PHP_Compat Package */
    /* http://pear.php.net/package/PHP_Compat */
    /* Provides missing functionality for older versions of PHP */
    if (!defined('FILE_USE_INCLUDE_PATH')) { define('FILE_USE_INCLUDE_PATH', 1); }
    if (!defined('LOCK_EX')) { define('LOCK_EX', 2); }
    if (!defined('FILE_APPEND')) { define('FILE_APPEND', 8); }
    if (!function_exists('file_put_contents')) {
     function file_put_contents($filename, $content, $flags = null, $resource_context = null) {
      // If $content is an array, convert it to a string
      if (is_array($content)) { $content = implode('', $content); }
      // If we don't have a string, throw an error
      if (!is_scalar($content)) {
       user_error('file_put_contents() The 2nd parameter should be either a string or an array',
       E_USER_WARNING);
       return false;
      }
      // Get the length of data to write
      $length = strlen($content);
      // Check what mode we are using
      $mode = ($flags & FILE_APPEND) ? 'a' : 'wb';
      // Check if we're using the include path
      $use_inc_path = ($flags & FILE_USE_INCLUDE_PATH) ? true : false;
      // Open the file for writing
      if (($fh = @fopen($filename, $mode, $use_inc_path)) === false) {
       user_error('file_put_contents() failed to open stream: Permission denied', E_USER_WARNING);
       return false;
      }
      // Attempt to get an exclusive lock
      $use_lock = ($flags & LOCK_EX) ? true : false ;
      if ($use_lock === true) {
       if (!flock($fh, LOCK_EX)) { return false; }
      }
      // Write to the file
      $bytes = 0;
      if (($bytes = @fwrite($fh, $content)) === false) {
       $errormsg = sprintf('file_put_contents() Failed to write %d bytes to %s', $length, $filename);
       user_error($errormsg, E_USER_WARNING);
       return false;
      }
      // Close the handle
      @fclose($fh);
      // Check all the data was written
      if ($bytes != $length) {
       $errormsg = sprintf('file_put_contents() Only %d of %d bytes written, possibly out of free disk space.', $bytes, $length);
       user_error($errormsg, E_USER_WARNING);
       return false;
      }
      // Return length
      return $bytes;
     }
    }
    
    /* *************************************************************************************************** */
    
    if ((($_SERVER['REQUEST_METHOD'] == 'POST') && ($_POST['choice'] != "") && ($_POST['question_number'] != "")) || ($_GET['show_results'] == '1')) {
     $display_results = true;
     if (($_GET['question'] != '') && ($_POST['choice'] == '')) {
      $_POST['question_number'] = $_GET['question'];
     }
    }
    else {
     $display_results = false;
    }
    
    // read the questions file
    if(file_exists($questions_file_location) && is_readable($questions_file_location)) {
     $questions_file_exists = true;
     $questions_file_contents = file($questions_file_location);
    
     if ($_POST['question_number'] != '') {
      $question_number = $_POST['question_number'];
     }
     else if ($_GET['question'] != '') {
      $question_number = $_GET['question'];
     }
     else {
      $question_number = rand(0, count($questions_file_contents)-1);
     }
    
     if ($question_number == (count($questions_file_contents)-1)) {
      $next_question = 0;
     }
     else {
      if ($_GET['question'] != '') { $next_question = $_GET['question'] + 1; }
      else { $next_question = $question_number + 1; }
     }
    
     $question_line = rtrim($questions_file_contents[$question_number]);
     $question_line_parts = explode(chr(9), $question_line);
     $questions_to_write = '<b>' . $question_line_parts[0] . '</b><br />' . "\r\n";
     $questions_to_write .= '    <ul id="poll_choices">' . "\r\n";
     $answer_count = count($question_line_parts) - 1;
     for ($i=0; $i<$answer_count; $i++) {
      $questions_to_write .= '     <li><input type="radio" name="choice" value="' . ($i + 1) . '" id="option_' . ($i + 1) . '"';
      if ($i==0) { $questions_to_write .= ' checked="checked"'; }
      $questions_to_write .= ' /><label for="option_' . ($i + 1) . '">' . $question_line_parts[$i+1] . '</label></li>' . "\r\n";
     }
     $questions_to_write .= '    </ul>' . "\r\n";
     $questions_to_write .= '    <input type="hidden" name="question_number" value="' . $question_number . '" />' . "\r\n";
     $questions_to_write .= '    <input type="submit" value="Vote" /> (<a href="?question=' . $question_number . '&amp;show_results=1">Display results</a>)' . "\r\n";
    }
    else {
     $questions_file_exists = false;
     $questions_to_write = '<span class="not_found">Questions file not found!</span>' . "\r\n";
    }
    
    if (($display_results == true) && ($questions_file_exists == true)) {
     if (file_exists($responses_file_location) == false) { create_empty_response_file(); }
     // read the responses file
     if(file_exists($responses_file_location) && is_readable($responses_file_location)) {
      $responses_file_contents = file($responses_file_location);
      // write to the responses file
      $response_file_to_write = '';
      $already_voted_text = '';
      $question_found = false;
      foreach($responses_file_contents as $line) {
       $line_parts = explode(chr(9), rtrim($line));
       if ($line_parts[0] == $_POST['question_number']) { // update this question
        $question_found = true;
        if ($_POST['choice'] != '') {
         if (file_exists($poll_ips_file_location) == false) { file_put_contents($poll_ips_file_location, ''); }
         // only if their IP is not found, then cast their vote
         $can_vote = true;
         $poll_file_to_write = '';
         $found_poll_question = false;
         $poll_ips_file_contents = file($poll_ips_file_location);
         foreach($poll_ips_file_contents as $poll_line) {
          $poll_line_parts = explode(chr(9), rtrim($poll_line));
          if ($poll_line_parts[0] == $_POST['question_number']) { // found the right question, grab ips
           $found_poll_question = true;
           for ($i=1; $i<count($poll_line_parts); $i++) {
            if ($_SERVER['REMOTE_ADDR'] == $poll_line_parts[$i]) { $can_vote = false; }
           }
           if ($can_vote == true) { // add the users ip
            $poll_file_to_write .= implode(chr(9), $poll_line_parts) . chr(9) . $_SERVER['REMOTE_ADDR'] . "\r\n";
           }
           else {
            $poll_file_to_write .= implode(chr(9), $poll_line_parts) . "\r\n";
           }
          }
          else {
           $poll_file_to_write .= implode(chr(9), $poll_line_parts) . "\r\n";
          }
         }
         if ($found_poll_question == false) { // add this question
          $poll_file_to_write .= $_POST['question_number'] . chr(9) . $_SERVER['REMOTE_ADDR'] . "\r\n";
         }
         file_put_contents($poll_ips_file_location, rtrim($poll_file_to_write));
         if ($can_vote == true) {
          $line_parts[$_POST['choice']] += 1;
         }
         else {
          $already_voted_text = '<span class="already_voted">** Your vote has already been cast in this poll **</span><br />' . "\r\n";
         }
        }
        $total_responses = 0;
        $answer_count = count($line_parts) - 1;
        for ($i=0; $i<$answer_count; $i++) {
         $answer_counts[$i] = $line_parts[$i+1];
         $total_responses += $answer_counts[$i];
        }
       }
       $response_file_to_write .= implode(chr(9), $line_parts) . "\r\n";
      }
      if ($question_found == false) { // append to response file
       $response_file_to_write .= $_POST['question_number'] . chr(9);
       $answer_count = count($question_line_parts) - 1;
       for ($i=0; $i<$answer_count; $i++) {
        if (($i+1) == $_POST['choice']) { $response_file_to_write .= '1'; $answer_counts[$i] = '1'; $total_responses = 1; }
        else { $response_file_to_write .= '0'; $answer_counts[$i] = '0'; }
        if ($i < ($answer_count-1)) { $response_file_to_write .= chr(9); }
       }
      }
      file_put_contents($responses_file_location, rtrim($response_file_to_write));
      $questions_to_write = $already_voted_text . '<b>' . $question_line_parts[0] . '</b><br />' . "\r\n";
      $questions_to_write .= '    <div id="poll_choices">' . "\r\n";
      $answer_count = count($question_line_parts) - 1;
      for ($i=0; $i<$answer_count; $i++) {
       if ($answer_counts[$i] != 1) { $s = 's'; } else { $s = ''; }
       if ($total_responses > 0) {
        $questions_to_write .= '     ' . $question_line_parts[$i+1] . ' (' . round(($answer_counts[$i] / $total_responses) * 100) . '%) (' . $answer_counts[$i] . ' vote' . $s . ')' . '<br />' . "\r\n";
       }
       else {
        $total_responses = 0;
        $questions_to_write .= '     ' . $question_line_parts[$i+1] . ' (0%) (0 votes)' . '<br />' . "\r\n";
       }
      }
      $questions_to_write .= '    </div>' . "\r\n";
      if ($total_responses != 1) { $ppl = 'people have'; } else { $ppl = 'person has'; }
      $questions_to_write .= '    In total, ' . $total_responses . ' ' . $ppl . ' responded to this poll.<br />' . "\r\n";
      $questions_to_write .= '    <input type="button" id="next_button" value="Take another poll" onclick="location.href=\'' . $_SERVER['PHP_SELF'] . '?question=' . $next_question . '\'" />' . "\r\n";
     }
     else { // create the responses file
      create_empty_response_file();
     }
    }
    
    function create_empty_response_file() {
     global $questions_file_exists, $question_line_parts, $responses_file_location;
     $response_file_to_write = '';
     if ($questions_file_exists == true) {
      $response_file_to_write = $_POST['question_number'] . chr(9);
      $answer_count = count($question_line_parts) - 1;
      for ($i=0; $i<$answer_count; $i++) {
       $response_file_to_write .= '0';
       if ($i < ($answer_count-1)) { $response_file_to_write .= chr(9); }
      }
     }
     file_put_contents($responses_file_location, $response_file_to_write);
    }
    
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
      <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
      <title>Poll Test</title>
      <style type="text/css">
       html, body, p, form, h1, h2, h3, h4, h5, h6, div, ol, ul, li { margin: 0px; padding: 0px; }
       body { margin: 8px; font: 10pt verdana, arial, sans-serif; }
       ul#poll_choices, div#poll_choices { margin-top: 7px; margin-bottom: 7px; list-style-type: none; }
       ul#poll_choices li { margin-bottom: 1px; }
       input#next_button { margin-top: 10px; }
       span.not_found { color: red; }
       span.already_voted { color: red; }
      </style>
      <script type="text/javascript">
       function poll_form_onsubmit() {
        if (document.cookie.indexOf('voted=<?php echo $question_number; ?>-1')>=0) { alert('You have already voted in this poll.'); return false; }
        else { document.cookie=document.cookie+'voted=<?php echo $question_number; ?>-1'; return true; }
        return true;
       }
      </script>
     </head>
     <body>
      <form action="" method="post" id="poll_form" onsubmit="return poll_form_onsubmit()">
    
        <?php echo $questions_to_write; ?>
    
      </form>
     </body>
    </html>
    
    Code (markup):
    This code will record everybody's responses (votes) in another text file called "poll_responses.txt". It will also prevent people from voting twice by storing their ip address in "poll_ips.txt". I also use cookies as a second form of protection to prevent people from voting twice. Let me know what you think :D
     
    brian394, Jun 7, 2006 IP
  10. squarehippies

    squarehippies Peon

    Messages:
    166
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Brian, thanks a lot for providing the sample. I have a website to include a poll in but it's not in PHP, I only use SSI to include the right navigation on every page so each page has .SHTML extension. My server supports PHP but I'm wondering if I can incorporate this script somehow in my SHTML website. If not possible, is there any alternative way like javascript, etc. because I use a simple javascript for my all my comments and found it quite easy to implement. Thanks in advance!
     
    squarehippies, Jul 18, 2006 IP
  11. chris20492002

    chris20492002 Guest

    Messages:
    1,136
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Poll is awesome but I think u might need to pm him the post is 45 days old
     
    chris20492002, Jul 18, 2006 IP