Hello,
I will try to make it as soon as i can.
It means you have allready create a database somewhere (i use hostinger) it accept PHP 7.2.
On start of layout check if that is the first run of the game (use a global variable "firstRun" = 0)
and run this action.
saveinfo.php must create it and save it to a folder to you site file manager.
DOMAIN_SCORE variable is your filepath.
We run this because we do not want every time the game end and return to menu to run the saveinfo.php
This php file look if a player compared by his uniqe id (uniqe id is the fb instant game id or whatever platfor you want) allredy exist.If its not exist it create a row in your database with (uniqe,name,pics,wins and loses)
Notice that i delete the first and last line (<php.....?>) because want let me insert it.
saveinfo.php
header('Access-Control-Allow-Origin: *');
$servername = "localhost";
$username = "***********";
$password = "**************";
$dbname = "*************";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$name = strip_tags(mysqli_real_escape_string($conn, $_GET['name']));
$uniqe = strip_tags(mysqli_real_escape_string($conn, $_GET['uniqe']));
$pics = strip_tags(mysqli_real_escape_string($conn, $_GET['pics']));
$win = strip_tags(mysqli_real_escape_string($conn, $_GET['win']));
$lose = strip_tags(mysqli_real_escape_string($conn, $_GET['lose']));
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, uniqe, pics FROM scores WHERE uniqe='$uniqe'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. "uniqe: " . $row["uniqe"]. "pics: " . $row["pics"]. " - Name: " . $row["name"]. " " . "<br>";
$sql =mysqli_query($conn, "UPDATE scores SET pics='$pics' WHERE uniqe='$uniqe'");
}
} else {
echo "0 results";
$sql = mysqli_query($conn, "INSERT INTO scores (name, uniqe, pics)
VALUES ('$name','$uniqe','$pics');" );
}
$conn->close();
On AJAX request "PostScore" complete we run this action
This will retrieve a number of entries you want.
getscores.php
header('Access-Control-Allow-Origin: *');
include 'login.php';
// Connect to server and select database.
$con = mysqli_connect($host, $db_username, $db_password, $db_name);
// Retrieve data from database
$sql = "SELECT *
FROM scores
ORDER BY score DESC
LIMIT 100000000"; // The 'LIMIT 100000000' part will only read 100 scores. Feel free to change this value
$result = mysqli_query($con, $sql);
// Start looping rows in mysql database.
while($rows = mysqli_fetch_array($result)){
echo $rows['name'] ." ". $rows['score'] ." ". $rows['pics'] ." ". $rows['uniqe'] ." ". $rows['lose'] ." ". $rows['win'] . "|";
}
// close MySQL connection
mysqli_close($con);
You notice include 'login.php';
Its a file that you create also in your file manager so no need every time to enter your informations.
login.php
header('Access-Control-Allow-Origin: *');
$host = "localhost"; // Host name
$db_username = "***********"; // Mysql username
$db_password = "***********"; // Mysql password
$db_name = "**************"; // Database name
$db_table = "*********"; // Table name
After you retreive the AJAX "getscores" request save the information to an array and run this action.
ran.php file will retreive your inforamtion
header('Access-Control-Allow-Origin: *');
$servername = "localhost";
$username = "***********";
$password = "****************";
$dbname = "****************";
$mysqli = new mysqli($servername, $username, $password, $dbname);
$uniqe = strip_tags(mysqli_real_escape_string($mysqli, $_GET['uniqe']));
if( $result = $mysqli->query( "SELECT * FROM scores WHERE uniqe ='".$uniqe."'") )
{
while( $row = $result->fetch_assoc() ) {
echo $row["name"]." ".$row["score"]." ".$row["win"]." ".$row["lose"]." ".$row["pics"]." ".$row["emo1"]." ".$row["emo2"]." ".$row["emo3"]." ".$row["spells"]." ". "<br>";
}
}
After that you need to run this action
This will run the rank1 php file and this file will rank all the players.
header('Access-Control-Allow-Origin: *');
$servername = "localhost";
$username = "*************";
$password = "*************";
$dbname = "*************";
$mysqli = new mysqli($servername, $username, $password, $dbname);
$uniqe = strip_tags(mysqli_real_escape_string($mysqli, $_GET['uniqe']));
$sql3 = $mysqli->query("SELECT * FROM scores ORDER BY score DESC") or die("Could not allocate information!");
$rank = 0;
while($row = mysqli_fetch_assoc($sql3)) {
$rank++;
if ($row['uniqe'] == $uniqe){
echo $rank;
}
}
The only think now is to send the score to the database.
We run this action
And the savewin php file is
header('Access-Control-Allow-Origin: *');
$servername = "localhost";
$username = "*************";
$password = "***************";
$dbname = "***************";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$name = strip_tags(mysqli_real_escape_string($conn, $_GET['name']));
$uniqe = strip_tags(mysqli_real_escape_string($conn, $_GET['uniqe']));
$win = strip_tags(mysqli_real_escape_string($conn, $_GET['win']));
$lose = strip_tags(mysqli_real_escape_string($conn, $_GET['lose']));
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, score, uniqe FROM scores WHERE uniqe='$uniqe'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. "uniqe: " . $row["uniqe"]. " - Name: " . $row["name"]. " " . $row["win"]. " " . $row["lose"]. "<br>";
$sql =mysqli_query($conn, "UPDATE scores SET win=win+'$win' WHERE uniqe='$uniqe'");
$sql =mysqli_query($conn, "UPDATE scores SET lose='$lose'+lose WHERE uniqe='$uniqe'");
$sql =mysqli_query($conn, "UPDATE scores SET score=(win/lose)*100 WHERE uniqe='$uniqe'");
}
} else {
echo "0 results";
$sql = mysqli_query($conn, "INSERT INTO scores (name, uniqe, win, lose)
VALUES ('$name','$uniqe','$win','$lose');" );
}
$conn->close();
Note that you maybe need to change the formula in the last php for sending the score.
I forgot to mention , when is the first run , "firstrun = 0" run the actions and set the "firstrun to 1"
When the players return to the menu and the firstrun = 1 just run the getscores php.