topical media & game development

talk show tell print

student-canvas-results.txt / txt



  <?php
          
          counter = 0;
          topScore = 0;
          duplicate = 0;
          names[0] = 0;
          scores[0] = 0;
          file = "highscores.txt";
          fh = fopen(file, "r") or exit("Unable to open file!");
          while(!feof(fh)) {
                  names[counter] = fgets(fh);
                  scores[counter] = (int)fgets(fh);
                  counter = counter + 1;
          }
          fclose(fh);
          
          for(k = 0; k < counter; k++) {
                  if(strcmp(rtrim(names[k]), _GET["name"]) == 0) {
                          if((int)scores[k] == (int)_GET["score"]) {
                                  duplicate = 1;
                          }
                  }
          }
          
          if((int)_GET["score"] == 0) {
                  duplicate = 1;
          }
          
          if(duplicate < 1) {
                  fh = fopen(file, 'a') or exit("can't open file");
                  stringData = strip_tags(_GET["name"]);
                  fwrite(fh, stringData);
                  stringData = "\n";
                  fwrite(fh, stringData);
                  stringData = _GET["score"];
                  fwrite(fh, stringData);
                  stringData = "\n";
                  fwrite(fh, stringData);
                  fclose(fh);
                  
                  names[counter] = strip_tags(_GET["name"]);
                  scores[counter] = (int)_GET["score"];
                  counter = counter + 1;
          }
          
          echo "<b>Highscores:</b>";
          echo "<table>";
          for(i = 0; i < 10; i++) {
                  for(j = 0; j < counter; j++) {
                          if(scores[j] > scores[topScore]) {
                                  topScore = j;
                          }
                  }
                  
                  if(scores[topScore] > 0) {
                          echo "<tr><td>#";
                          echo i + 1;
                          echo ".</td><td>";
                          echo names[topScore];
                          echo "</td><td>";
                          echo scores[topScore];
                          echo "</td></tr>";
                          scores[topScore] = 0;
                  }
          }
          echo "</table>";
          echo "<br><br><a href='CD05.html'>Go back</a>"
          echo "<a href='index.html'>Go back to main page</a>"
  ?>


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.