我现在遇到一些问题,我必须与员工一起使用数据库。这些雇员有不同的数据,这些是字符串和整数,数组中的例子是:Naam:Pino |球队:2 | VerkoopA:12 | VerkoopB:12 | NPS:40 | conversie:10。
这是一个只有一名员工的例子,数据库中有更多,我已经在代码中拆分了它们。我想要做的是用这些数据制作排行榜,但是我不知道如何分离数组中的不同项目,如果我已经分离了这个,我怎么能对数组进行排序以查看谁拥有最多的saleA和谁拥有最多的销售B,所以我可以使用过滤器作为排行榜。
还有一种简单的方法来建立可以显示一定数量员工的可视排行榜吗?因为我现在显示它的方式它没有显示任何内容。
我正在使用Unity和我自己的服务器和Xampp
亲切的问候,
迪拉姆
脚本:
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'] . ";";
}
}
?>
修改
程序员帮忙后,我决定使用json
我现在遇到一些问题,我必须与员工一起使用数据库。这些雇员有不同的数据我的PHP文件的输出是:
[{ “ID”: “1”, “组”: “2”, “VerkoopA”: “35”, “VerkoopB”: “12”, “NPS”: “25”, “Conversie”:” 18 “},{” ID “:” 2" , “团队”: “1”, “VerkoopA”: “55”, “VerkoopB”: “2”, “NPS”: “12”, “Conversie”:” 40 “},{” ID “:” 3" , “队”: “2”, “VerkoopA”: “12”, “VerkoopB”: “12”, “NPS”: “40”, “Conversie”:” 10" }]
我想要做的是从这些数据中取出排行榜但是我不知道如何分离数组中的不同项通过使用JsonHelper我得到一个错误:ArgumentException:JSON必须代表一个对象
脚本:
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);
?>
答案 0 :(得分:1)
由于您可以访问服务器和服务器脚本(php),因此应使用json
或xml
执行此操作,而不是使用|
分隔数据。
以json的形式从数据库发送数据。在Unity端,使用WWW
类接收数据,然后使用JsonUtility
将json转换回类。
Unity的JsonUtility
不支持数组,所以你需要一个包装器。您可以获取允许json数组here 的JsonHelper
类。
这就是我上面描述的内容:
[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;
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方面,你必须使用json_encode
函数将数据库信息编码为json数组,然后再发送给Unity。不是php人,但你可以详细了解here和here。
对结果进行排序:
您可以在使用
从服务器接收数据后按ID
排序
leaderboard = leaderboard.OrderBy(c => c.ID).ToArray();
或NPS
leaderboard = leaderboard.OrderBy(c => c.NPS).ToArray();
这必须在foreach
循环之前完成。
修改强>
由php生成的json无法直接在Unity中准备好。不擅长PHP,无法修复php端的json数据。虽然,我可以在Unity方面修复它。
在收到的字符串前添加{"Items":
,然后在其末尾添加}
。
我为此做了一个简单的功能:
string fixJson(string value)
{
value = "{\"Items\":" + value + "}";
return value;
}
将该功能包含在您的脚本中,然后直接替换
string leaderboardsDataString = leaderboardsData.text;
与
string leaderboardsDataString = fixJson(leaderboardsData.text);
现在一切都应该有效。