Guest User

Untitled

a guest
Jun 5th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>ATOMIC SUPER BOSS</title>
  4. </head>
  5. <body>
  6. <?php
  7.  
  8. //The way this script works is pretty easy
  9. //To make the script put or update a player in the list call the script as: highscore.php?action=set&name=putnamehere&score=scorehere
  10. //To make the script output the top 5 people call it either: highscore.php or highscore.php?action=top
  11.  
  12. //first connect to database
  13. $host = "";
  14. $user = "";
  15. $pass = "";
  16. $scores_db = "";
  17.  
  18. $connect=mysql_connect ($host,$user,$pass) or die ('I cannot connect to the database because: ' . mysql_error());
  19. mysql_select_db ($scores_db);
  20.  
  21. //set variables for easier use inside strings
  22. $score=$_GET["score"];
  23. $name=$_GET["name"];
  24. $action=$_GET["action"];
  25. $nummer=1;
  26.  
  27. //this function is the responsible of modifing the player in the table
  28. function modify_table()
  29. {
  30. //first the query try to get a riw where the name of the player is = to the name in the url
  31. $query = "SELECT name FROM shmup WHERE name='".$_GET["name"]."'";
  32. $res = mysql_query($query) or die("Couldn't execute $query: ".mysql_error());
  33.  
  34. //if the query returns 0 rows the player doesn't exist, if it returns 1 the player already exists
  35. if (mysql_num_rows($res)==0)
  36. {
  37. //the query makes a new row with the player name and the score
  38. $query = "INSERT INTO shmup VALUES('".$_GET["name"]."',".$_GET["score"].")";
  39. mysql_query($query) or die("Couldn't execute $query: ".mysql_error());
  40. }
  41. else
  42. {
  43. //the query looks for the row with name=name in the url and updates his score
  44. $query = sprintf("UPDATE `shmup` SET `score`=%d WHERE `name`='%s' AND `score`<%d", $_GET['score'], $_GET['name'], $_GET['score']);
  45. mysql_query($query) or die("Couldn't execute $query: ".mysql_error());
  46. }
  47. }
  48.  
  49. function show_top()
  50. {
  51. //we make mysql return all rows in the database, but ordered descendent by the socre column and only the firt 5
  52. //so, the 5 highest scores will be showed, change the 5 with the number of top players you want to show
  53. $query = "SELECT name, score FROM shmup ORDER BY score DESC LIMIT 250";
  54. $res = mysql_query($query) or die("Couldn't execute query: ".mysql_error());
  55.  
  56. //this echos the html code for starting a table
  57. echo "
  58. <html>
  59. <body bgcolor=#FFFFFF text=#000000 link=#222222 alink=#222222 vlink=#222222>
  60. <center><b>ATOMIC SUPER BOSS</b><br/>
  61. <table border=0 width=240px>
  62. <tr>
  63. <td align='left' bgcolor=#CCCCCC style='font-color: white; font-family:Georgia'> <b>#</b> </td><td align='left' bgcolor=#CCCCCC style='font-color: white; font-family:Georgia'> <b>name</b> </td> <td align='left' bgcolor=#CCCCCC style='font-color: black; font-family:Georgia'> <b>score</b> </td>
  64. </tr>
  65. ";
  66. while ($user = mysql_fetch_assoc($res))
  67. {
  68. //the while loop is complicated, took me a week to fully understand how it works
  69. //It'll output a row with the user name and score
  70.  
  71.  
  72.  
  73. echo "<tr>
  74. <td align='left' bgcolor=#EEEEEE style='font-color: white; font-family:verdana' >
  75. ".$nummer."
  76. </td>
  77. <td align='left' bgcolor=#EEEEEE style='font-color: white; font-family:verdana' >
  78. ".$user["name"]."
  79. </td> <td align='left' bgcolor=#EEEEEE style='font-color: white; font-family:verdana'>
  80. ".$user["score"]."
  81. </td>
  82. </tr>";
  83. }
  84. $nummer = $nummer+1;
  85. echo " </table> </center>";
  86. }
  87.  
  88. //this switch will look at the $action variable I declared
  89. //IF action=set means the person wants to input a player score, so we call modify_table()
  90. //If action=top means the person wants to see the top5 players
  91. //If none of the above simply show the top players
  92. switch ($action)
  93. {
  94. case "set": modify_table(); break;
  95. case "top": show_top(); break;
  96. default: show_top(); break;
  97. }
  98.  
  99. //if you want to make the show_top() function alone in one php file simply copy the code before the function modify table()
  100. //put the function code in the middle and put mysql_close()
  101.  
  102. //close the connection, you MUSTN't forget to do this after you open a connection
  103. mysql_close($connect);
  104. ?>
  105.  
  106. </center> </body></html>
Add Comment
Please, Sign In to add comment