Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>ATOMIC SUPER BOSS</title>
- </head>
- <body>
- <?php
- //The way this script works is pretty easy
- //To make the script put or update a player in the list call the script as: highscore.php?action=set&name=putnamehere&score=scorehere
- //To make the script output the top 5 people call it either: highscore.php or highscore.php?action=top
- //first connect to database
- $host = "";
- $user = "";
- $pass = "";
- $scores_db = "";
- $connect=mysql_connect ($host,$user,$pass) or die ('I cannot connect to the database because: ' . mysql_error());
- mysql_select_db ($scores_db);
- //set variables for easier use inside strings
- $score=$_GET["score"];
- $name=$_GET["name"];
- $action=$_GET["action"];
- $nummer=1;
- //this function is the responsible of modifing the player in the table
- function modify_table()
- {
- //first the query try to get a riw where the name of the player is = to the name in the url
- $query = "SELECT name FROM shmup WHERE name='".$_GET["name"]."'";
- $res = mysql_query($query) or die("Couldn't execute $query: ".mysql_error());
- //if the query returns 0 rows the player doesn't exist, if it returns 1 the player already exists
- if (mysql_num_rows($res)==0)
- {
- //the query makes a new row with the player name and the score
- $query = "INSERT INTO shmup VALUES('".$_GET["name"]."',".$_GET["score"].")";
- mysql_query($query) or die("Couldn't execute $query: ".mysql_error());
- }
- else
- {
- //the query looks for the row with name=name in the url and updates his score
- $query = sprintf("UPDATE `shmup` SET `score`=%d WHERE `name`='%s' AND `score`<%d", $_GET['score'], $_GET['name'], $_GET['score']);
- mysql_query($query) or die("Couldn't execute $query: ".mysql_error());
- }
- }
- function show_top()
- {
- //we make mysql return all rows in the database, but ordered descendent by the socre column and only the firt 5
- //so, the 5 highest scores will be showed, change the 5 with the number of top players you want to show
- $query = "SELECT name, score FROM shmup ORDER BY score DESC LIMIT 250";
- $res = mysql_query($query) or die("Couldn't execute query: ".mysql_error());
- //this echos the html code for starting a table
- echo "
- <html>
- <body bgcolor=#FFFFFF text=#000000 link=#222222 alink=#222222 vlink=#222222>
- <center><b>ATOMIC SUPER BOSS</b><br/>
- <table border=0 width=240px>
- <tr>
- <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>
- </tr>
- ";
- while ($user = mysql_fetch_assoc($res))
- {
- //the while loop is complicated, took me a week to fully understand how it works
- //It'll output a row with the user name and score
- echo "<tr>
- <td align='left' bgcolor=#EEEEEE style='font-color: white; font-family:verdana' >
- ".$nummer."
- </td>
- <td align='left' bgcolor=#EEEEEE style='font-color: white; font-family:verdana' >
- ".$user["name"]."
- </td> <td align='left' bgcolor=#EEEEEE style='font-color: white; font-family:verdana'>
- ".$user["score"]."
- </td>
- </tr>";
- }
- $nummer = $nummer+1;
- echo " </table> </center>";
- }
- //this switch will look at the $action variable I declared
- //IF action=set means the person wants to input a player score, so we call modify_table()
- //If action=top means the person wants to see the top5 players
- //If none of the above simply show the top players
- switch ($action)
- {
- case "set": modify_table(); break;
- case "top": show_top(); break;
- default: show_top(); break;
- }
- //if you want to make the show_top() function alone in one php file simply copy the code before the function modify table()
- //put the function code in the middle and put mysql_close()
- //close the connection, you MUSTN't forget to do this after you open a connection
- mysql_close($connect);
- ?>
- </center> </body></html>
Add Comment
Please, Sign In to add comment