I am building an online tests system for a school, where the questions and answers are saved in an xml file on the school website server, the problem is i don't know how can i save the xml file on the server and how can i call it back ?
My Code till now
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using System;
public class XMLSerializer : MonoBehaviour {
public string studentname;
public void fff() {
string path = "d:/" + "studentanswer"+ ".xml";
Debug.Log (path);
XmlSerializer serializer = new XmlSerializer (typeof(Level1));
Level1 ak47 = new Level1{ Items=new [] {new mainitems { Question = "a", Answer = "2" },new mainitems { Question="b",Answer="5"}}};
StreamWriter writer = new StreamWriter(path);
serializer.Serialize(writer, ak47);
writer.Close();
}
}
You're going to want some kind of datalayer to talk to the database!
Just some method that wants to use the datalayer:
// This would probably be your fff() method.
public void SaveXml(string xmlData) {
StartCoroutine(DataLayer.SaveXml(xmlData));
}
Then our static class that will talk to the database-layer:
using UnityEngine;
static class DataLayer
{
public static string url = "http://www.yourdomain.com/DatabaseLayer.php";
public static IEnumerator SaveXml(string xmlData)
{
WWWForm wwwForm = new WWWForm();
wwwForm.AddField("action", "save");
wwwForm.AddField("xmldata", xmlData);
WWW www = new WWW(url, wwwForm);
// Wait until the request has been sent and a response from the server been recieved:
yield return www;
if (www.error == null) {
// Success! Debug.Log what the server displays
Debug.Log(www.text);
}
}
and then the DatabaseLayer.php (that we're previously referencing in url
) (doesn't have to be php, it can be any language of your choice that can recieve a POST and talk to the database. Php is just very simple to set up) A quick example without security of how it could look like:
$con = new mysqli("your.host","your_user","your_pwd","your_db");
if ($con->connect_errno)
{
echo "Failed to connect to db: " . $con->connect_error;
}
// If xmlData has been sent and the POST parameter "action" == "save"
if ($_POST['xmlData'] && $_POST['action'] == "save")
{
$xmlData = $_POST['xmlData'];
$query = 'insert into YourTable (xmlDataColumnName) values ("' . $xmlData . '")';
if ($res = $con->query($query)) {
echo 'Level saved';
} else {
echo $con->error;
}
}
A simple overview of the flow would be:
Unity MonoBehaviour Game Scripts --> Static Datalayer class <--> Php <--> Database
If you want further information and examples on how to use Coroutines and getting information when the coroutine is done in the MonoBehaviour (adding the "arrow" back from Static datalayer class to Unity MonoBehaviour Game Scripts in the overview above) check this little guide on callbacks I wrote!
Regarding getting information from the database, it's the same thing but instead of just inserting in the database in the php-layer, you select it from the database and print it, which will make us recieve that information in the Static DataLayer Class in the www.text
. www.text
is everything that is displayed on the website you're requesting.
You will need some server side script in a Web server (PHP for example) and then you can call those scripts using the WWW class.
Check some PHP+unity tutorial and you will get a good start