· 4 years ago · Jan 21, 2021, 02:32 AM
1<?php
2/*
3 Online HighScore App. Will Retrieve The Top Scores of the DB
4 to display back to frontend program or anywhere,
5
6 Can be used on the frontend of your website
7 or through GM's Async.
8*/
9
10 // Connect to database
11 $db = new PDO('mysql:host=localhost;dbname=appTable');
12
13 // Check secret key, if correct, then get names and scores
14 $has_found = 0;
15 //Change The Key To make it secure. Needs to match to pull otherwise exit
16 $secret_key = "1234";
17
18 if($secret_key == $_POST['secret_key'])
19 {
20 // Get all data from the table, ordering from best to worst
21 $sql = "SELECT * FROM `OnlineHighscores` ORDER BY `score` DESC";
22 $stmt = $db->prepare($sql);
23 $stmt->execute();
24
25 // Fetch the result into a nice format EXAMPLE: 1. Guest2837 100
26 // no_lines is the length of the list, generally you will want a top 10
27 $line = 1;
28 $no_lines = $_POST['no_lines']; //Limit of Scores(Needs To be passed internally by GM)
29 //If you somehow forget to pass the app will default to 10
30 if ($no_lines == 0 || empty($no_lines))
31 {
32 $no_lines = 10;
33 }
34
35 //Begin Pulling
36 while($row = $stmt->fetch(PDO::FETCH_ASSOC))
37 {
38 // We only want a top no_lines
39 if($line <= $no_lines)
40 {
41 // Check if you are in the top no_lines
42 if($row['name'] == $_POST['name'])
43 {
44 $has_found = 1;
45 }
46 // Echo the top no_lines list
47 echo $line . ".-" . $row['name'] . "-" . $row['score'] . "|";
48 $line += 1;
49 }
50 else
51 {
52 // When you are not in the top no_lines list, search for your record
53 if($has_found == 0)
54 {
55 if($row['name'] == $_POST['name'])
56 {
57 $has_found = 1;
58 echo $line . ".-" . $row['name'] . "-" . $row['score'] . "|";
59 break;
60 }
61 $line += 1;
62 }
63 else
64 {
65 break;
66 }
67 }
68 }
69
70 // Send some empty lines if there are not enough scores
71 if($line <= $no_lines)
72 {
73 for($i = $line; $i<=$no_lines; $i++)
74 {
75 echo $i . ".--|";
76 }
77 }
78 }
79 else{
80 echo 'Oops! There Seems To Be An Error Pulling HighScores At The Moment.';
81 }
82?>