Search code examples
phpfile-iosurvey

PHP counter for multiple variables for website


I am looking to setup a Server that accepts a URL with a few variables in it which I'll use the $_GET statement to obtain. These variables will increment counters stored on the server. I was going to write these files to a file and then open it/write to it kind of like this http://www.developingwebs.net/phpclass/hitcounter.php however this is only showing for one variable. I changed the code to reflect multiple variables but am not entirely sure how to write the multiple variables to the file. I was using this:

$counters = ("counter.txt");
$increment = file($counters);
/* Bunch of if else ladders checking the $_GET statements and
   incrementing $increments[] accordingly */
for($i; $i < 16; $i++)          //write variables to file
    fputs($fp, $increment[$i]);

Where $fp points to the text file I was using and $increment[ ] holds the variables being incremented and such. So would this work? And would this work with multiple people accessing this URL at the same time? It needs to keep an accurate count of all the variables regardless of how many people are accessing the page.

Example: a survey submitted online with 4 questions. Each response has 4 options to it so in total 16 variables being stored. People will be submitting their responses to the server randomly and possibly at the same time. I need to parse their response and update the counter accordingly even when multiple people are submitting at the same time.

Thanks for any help, hope I supplied enough detail but if not just ask questions.

EDIT: The URL is being sent from an Android device to the Server I don't know if that changes anything but just wanted to be clear. The Android device is submitting the survey responses.


Solution

  • Building on Graham's comment, you'd be far better off letting your database server handle responses, and building your totals as part of a reporting system rather than part of the form submission process.

    Here's an example, in sort-of-meta code. First, your HTML form:

    <form method="GET"> <!-- though I recommend POST instead -->
     <input type="checkbox" name="ch[1]"> Checkbox 1
     <input type="checkbox" name="ch[2]"> Checkbox 2
     <input type="checkbox" name="ch[3]"> Checkbox 3
    </form>
    

    Then, the PHP that receives the form:

    <?php
    
      $qfmt = "INSERT INTO answers (question, answer) VALUES ('%s, '%s')";
    
      foreach ($ch as $key => $value) {
        if ($value == 'Yes') {
          $query = sprintf($qfmt, $key, $value);
          mysql_query($query);
        }
      }
    
      print "<p>Thanks!</p>\n";
    
    ?>
    

    Lastly, to gather your totals:

    SELECT COUNT(*) FROM answers WHERE question = '1';
    SELECT COUNT(*) FROM answers WHERE question = '2';
    SELECT COUNT(*) FROM answers WHERE question = '3';
    

    You can adapt this to handle other form input as well, and perhaps store a long-lived session cookie to let you detect whether the same browser gets used to fill out your form multiple times.