· 7 years ago · Oct 24, 2018, 09:32 PM
1<?php
2 // Connect to database
3 $db = new PDO('mysql:host=localhost;dbname=my_jeffdev', 'jeffdev');
4
5 // Check secret key, if correct, then get names and scores
6 $has_found = 0;
7 $secretKey = "MySecretKey";
8 if($secretKey == $_POST['MySecretKey'])
9 {
10 // Get all data from the table, ordering from best to worst
11 $sql = "SELECT * FROM Micro ORDER BY score DESC";
12 $stmt = $db->prepare($sql);
13 $stmt->execute();
14
15 // Fetch the result into a nice format EXAMPLE: 1. Guest2837 100
16 // no_lines is the length of the list, generally you will want a top 10
17 $line = 1;
18 $no_lines = $_POST['no_lines'];
19 while($row = $stmt->fetch(PDO::FETCH_ASSOC))
20 {
21 // We only want a top no_lines
22 if($line <= $no_lines)
23 {
24 // Check if you are in the top no_lines
25 if($row['name'] == $_POST['name'])
26 {
27 $has_found = 1;
28 }
29 // Echo the top no_lines list
30 echo $line . ".-" . $row['name'] . "-" . $row['score'] . "|";
31 $line += 1;
32 }
33 else
34 {
35 // When you are not in the top no_lines list, search for your record
36 if($has_found == 0)
37 {
38 if($row['name'] == $_POST['name'])
39 {
40 $has_found = 1;
41 echo $line . ".-" . $row['name'] . "-" . $row['score'] . "|";
42 break;
43 }
44 $line += 1;
45 }
46 else
47 {
48 break;
49 }
50 }
51 }
52 if($line <= $no_lines)
53 {
54 for($i = $line; $i<=$no_lines; $i++)
55 {
56 echo $i . ".-" . "" . "-" . "" . "|";
57 }
58 }
59 }
60?>