I'm having a bit of a problem at the moment I have to work with a database with employees. these employes have different data these are strings and ints the example of what is in the array is: Naam:Pino | Team:2 | VerkoopA:12 | VerkoopB:12 | NPS:40 | conversie:10.
this is an example of only one employee there are more in the database and I already split them in the code. The thing I want to do is make a leaderboards out of this data however I do not know how to seperate the different items in the Array and if I have seperated this how can I sort the array to see who hase the most saleA and who has the most saleB so I can make this a leaderboard with a filter.
Also is there an easy way to have a visual leaderboards that can display a certain amount of employees? Because the way I displayed it now it shows nothing.
I'm using Unity and my own server with Xampp
With kind regards,
Dh
Script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Leaderboard : MonoBehaviour
{
// Array of all the employees
public string[] leaderboard;
Text txt;
IEnumerator Start()
{
// Getting the leaderboard data from mySQL.
WWW leaderboardsData = new WWW("http://localhost/leaderboards.php");
yield return leaderboardsData;
// Split every employee in the Array so they are all apart from each other. |
// an example Message Naam:Pino | Team:2 | VerkoopA:12 | VerkoopB:12 | NPS:40 | Conversie:10
string leaderboardsDataString = leaderboardsData.text;
leaderboard = leaderboardsDataString.Split(';');
// for every employee in the array print every employee's data.
foreach (string employee in leaderboard)
{
string employeeApart = employee;
DisplayLeaderboards(employeeApart);
}
}
// Display the leaderboards 1 by 1.
void DisplayLeaderboards(string employeeApart)
{
print(employeeApart);
txt = gameObject.GetComponent<Text>();
txt.text = employeeApart;
}
string GetDataValue(string data, string index)
{
string value = data.Substring(data.IndexOf(index) + index.Length);
if (value.Contains("|")) value = value.Remove(value.IndexOf("|"));
return value;
}
}
PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbName = "test3";
//Make Connection
$conn = new mysqli($servername, $username, $password, $dbName);
//Check Connection
if(!$conn){
die("Connection Failed. ". mysqli_connect_error());
}
$sql = "SELECT ID, Team, VerkoopA, VerkoopB, NPS, Conversie FROM Leaderboards";
$result = mysqli_query($conn ,$sql);
if(mysqli_num_rows($result) > 0){
//show data for each row
while($row = mysqli_fetch_assoc($result)){
echo "ID:".$row['ID'] . "|Team:".$row['Team']. "|VerkoopA:".$row['VerkoopA']. "|VerkoopB:".$row['VerkoopB'] . "|NPS:".$row['NPS']. "|Conversie:".$row['Conversie'] . ";";
}
}
?>
EDIT:
After programmers help, I decided to use json
I'm having a bit of a problem at the moment I have to work with a database with employees. these employes have different data the output of my PHP file is:
[{"ID":"1","Team":"2","VerkoopA":"35","VerkoopB":"12","NPS":"25","Conversie":"18"},{"ID":"2","Team":"1","VerkoopA":"55","VerkoopB":"2","NPS":"12","Conversie":"40"},{"ID":"3","Team":"2","VerkoopA":"12","VerkoopB":"12","NPS":"40","Conversie":"10"}]
The thing I want to do is make a leaderboards out of this data however I do not know how to seperate the different items in the Array By using the JsonHelper I get an error: ArgumentException: JSON must represent an object
Script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Leaderboard: MonoBehaviour
{
// Array of all the employees
[System.Serializable]
public class LeaderBoard
{
public string ID;
public string Team;
public string VerkoopA;
public string VerkoopB;
public string NPS;
public string Conversie;
}
Text txt;
IEnumerator Start()
{
// Getting the leaderboard data from mySQL.
WWW leaderboardsData = new WWW("http://localhost/leaderboards.php");
yield return leaderboardsData;
string leaderboardsDataString = leaderboardsData.text;
print(leaderboardsDataString);
LeaderBoard[] leaderboard;
leaderboard = JsonHelper.FromJson<LeaderBoard>(leaderboardsDataString);
// for every employee in the array print every employee's data.
foreach (LeaderBoard employee in leaderboard)
{
DisplayLeaderboards(employee);
}
}
// Display the leaderboards 1 by 1.
void DisplayLeaderboards(LeaderBoard employeeApart)
{
Debug.Log("ID: " + employeeApart.ID);
Debug.Log("Team: " + employeeApart.Team);
Debug.Log("VerkoopA: " + employeeApart.VerkoopA);
Debug.Log("VerkoopB: " + employeeApart.VerkoopB);
Debug.Log("NPS: " + employeeApart.NPS);
Debug.Log("Conversie: " + employeeApart.Conversie);
}
}
PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbName = "test3";
//Make Connection
$conn = new mysqli($servername, $username, $password, $dbName);
//Check Connection
if(!$conn){
die("Connection Failed. ". mysqli_connect_error());
}
$sql = "select * from Leaderboards";
$result = mysqli_query($conn, $sql) or die("Error in Selecting " . mysqli_error($conn));
//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);
?>