Search Unity

Returning and storing an array using WWW.Text

Discussion in 'Scripting' started by Braveheart, Sep 12, 2012.

  1. Braveheart

    Braveheart

    Joined:
    Sep 6, 2012
    Posts:
    17
    I'm looking for a little help with returning an array of strings from a PHP script using WWW.Text and storing each character's name in an array named Characters for later using as separate GUI buttons.

    The following is my current code, which isn't returning anything:

    Code (csharp):
    1.  
    2.  
    3. void GetCharacters() {
    4.        
    5.     WWWForm form = new WWWForm();
    6.         form.AddField("username", Username);
    7.         WWW www = new WWW(url, form);
    8.     StartCoroutine(WaitForRequest(www));
    9.  
    10.     }
    11.  
    12.       IEnumerator WaitForRequest(WWW www)
    13.     {
    14.         yield return www;
    15.  
    16.         if (www.error == null)
    17.         {
    18.             Debug.Log("Database connection working! Response: " + www.text);
    19.             int i = -1;
    20.             Response = www.text;
    21.            
    22.             if(Response != "No characters found") {
    23.                 Response = "";
    24.                 while (www.text != "Done")
    25.             {
    26.                Characters[++i] = www.text;
    27.                 }
    28.             }
    29.            
    30.             www.Dispose();
    31.            
    32.         } else {
    33.             Debug.Log("Database connection error: "+ www.error);
    34.         }  
    35.     }

    The section of PHP code the above is reading from:

    Code (csharp):
    1. {
    2.  
    3.  
    4.             $datas = @mysql_fetch_array($result_id);
    5.  
    6.             if(!strcmp($nick, $datas["owner"])) {
    7.  
    8.                 echo "Character list successful";
    9.                
    10.                 //CHECK VARIOUS STATUSES
    11.                 if($datas['lives'] == 0){
    12.                   echo "This character has been permanently killed";
    13.                   mysql_query("UPDATE users SET last_login = '$date' WHERE username='$nick'") or die(mysql_error());
    14.                 }
    15.                
    16.                 ELSE{
    17.                while($datas) echo $datas['name'];
    18.                echo "Done";
    19.                 }              
    20.             }
    21.  
    22.         } else {
    23.  
    24.             echo "No characters found";
    25.  
    26.         }

    My thinking was the PHP script would send each individual echo to the C# code to be processed individually, this doesn't appear to be the case.
     
  2. Braveheart

    Braveheart

    Joined:
    Sep 6, 2012
    Posts:
    17
    Well I managed to figure it out after a lot of trial and error. It's actually quite simple, for the benefit of others this is how I did it.

    I used PHP to return every character name (owned by the requesting player) with a while loop:

    Code (csharp):
    1.  
    2.  $SQL = mysql_query("SELECT * FROM characters WHERE owner = '$username'", $connect) or die("DATABASE ERROR!:" . mysql_error());
    3.  
    4. while($row = mysql_fetch_array($SQL)) {
    5.                 $name = $row['name'];
    6.                 $lives = $row['lives'];
    7.                
    8.                 echo("$name;");
    9. }
    In C# I then used .www.text.Split to separate the returned values like so:


    Code (csharp):
    1. if(www.text == "No characters found")
    2.                 characters = null;
    3.            
    4.             else
    5.             characters = www.text.Split(";"[0]);
    6.            
    7.             www.Dispose();
    8.  
    (characters is a string array (string[] characters).)

    I then used a for loop to loop through the array and display a button for each character found in the database (length minus 1 otherwise it will always display one blank button):

    Code (csharp):
    1. for (int i = 0; i < characters.Length-1; i++) {    
    2.            
    3.             if(GUILayout.Button(characters[i], GUILayout.Height(60)) {
    4.                            
    5.                 string name = characters[i];
    6.                
    7.                 if(name.IndexOf("killed") == -1){
    8.                     Persist.CharacterName = name;
    9.                     chosen = true;
    10.                 }
    11.  
    12.                 GUILayout.Label(Persist.CharacterName + " selected");          
    13.             }
    14.                
    15.         }
     
  3. keithsoulasa

    keithsoulasa

    Joined:
    Feb 15, 2012
    Posts:
    2,126
    The problem their is you have to run several SQL queries just to get that one array .
    Doing something like array explode, adding a "," , then getting that whole string and spliting it would work too .

    Of course Json is a magical encription that would be far better at this .
     
  4. Braveheart

    Braveheart

    Joined:
    Sep 6, 2012
    Posts:
    17
    I have worked it out already, read my second post. Thanks for the help anyway.