Search Unity

Split php/sql values into variables.

Discussion in 'Scripting' started by ikaruga, Jun 9, 2007.

  1. ikaruga

    ikaruga

    Joined:
    Nov 18, 2006
    Posts:
    85
    Hi,

    You re probablly all sick of hearing me about it on irc, but yea. after 2 days of trying different techniques and code, we ve gotten nothing.

    Let say I have php values divided by a "|" .
    FILE: http://syntheticswampfields.net/ssf_unity.php

    I can read them in unity and display them as GUItext
    FILE: http://syntheticswampfields.net/ssf_uni.html

    But how can i split each values ( playername, stat1,stat2,stat3 ) into javascript values useables in if() statements later to display different things relatively to the values in my sql database. like if (stats2 > 10) { }; and stats2 would be the 3 value after the pipes ( | ). I hope this makes sense

    If you figure this out you ll save me days/nights : )

    Thanks a lot
     
  2. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    I've been using Mono's Regex class to split and match text. If you just want to split on a "|" character you can use myStringVar.Split(new char[]='|').... I'm not sure of the exact syntax, you'll have to play around w/ it. Split takes an char array that lists the delimiters you want to use.
     
  3. ikaruga

    ikaruga

    Joined:
    Nov 18, 2006
    Posts:
    85
    I remember trying this and not working well. Here's my code, I am totally clueless on what to do anymore.

    Code (csharp):
    1. var cube1 : GameObject;
    2. var textstats : GameObject;
    3. var playerName : String;
    4.  
    5. function Start() {
    6.     getScores();
    7.     if ( playerName == "ika" || "IKA") {   
    8.     cube1.transform.localScale.x = 30;
    9.     }
    10. }
    11.  
    12. // Get the scores from the MySQL DB to display in a GUIText.
    13. function getScores() {
    14.  
    15.     gameObject.guiText.text = "Loading Scores";
    16.  
    17.         /****************************************** GETS NAME********************************/
    18.         var highscore_url="ssf_unity.php?row='name'&record=1";
    19.         hs_get = WWW(highscore_url);
    20.         yield hs_get;
    21.  
    22.         if(hs_get.error) {
    23.             print("There was an error getting the high score: " + hs_get.error);
    24.         } else {
    25.             playerName = hs_get.data;
    26.         }
    27.         /****************************************** GETS SCORE ********************************/
    28.         highscore_url="ssf_unity.php?row='score'&record=1";
    29.         hs_get = WWW(highscore_url);
    30.         yield hs_get;
    31.  
    32.         if(hs_get.error) {
    33.             print("There was an error getting the high score: " + hs_get.error);
    34.         } else {
    35.  
    36.             var score = hs_get.data;
    37.         }
    38.  
    39.         /****************************************** GETS STATS1 ********************************/
    40.         highscore_url="ssf_unity.php?row='stats1'&record=1";
    41.         hs_get = WWW(highscore_url);
    42.         yield hs_get;
    43.  
    44.  
    45.         if(hs_get.error) {
    46.             print("There was an error getting the high score: " + hs_get.error);
    47.         } else {
    48.  
    49.             var stats1 = hs_get.data;
    50.         }
    51.  
    52.         /****************************************** GETS STATS2 ********************************/
    53.         highscore_url="ssf_unity.php?row='stats2'&record=1";
    54.         hs_get = WWW(highscore_url);
    55.         yield hs_get;
    56.  
    57.         if(hs_get.error) {
    58.             print("There was an error getting the high score: " + hs_get.error);
    59.         } else {
    60.  
    61.             var stats2 = hs_get.data;
    62.         }
    63.         gameObject.guiText.text = playerName+" "+score+" "+stats1+" "+stats2;
    64.         }
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It should work fine...

    Code (csharp):
    1. var someString = "foo|10|20|30";
    2. var splitString : String[] = someString.Split("|"[0]);
    3. var myName = splitString[0];
    4. var scores = new int[splitString.Length-1];
    5. for (i = 0; i < splitString.Length-1; i++) {
    6.      scores[i] = parseInt(splitString[i+1]);
    7. }
    --Eric
     
  5. ikaruga

    ikaruga

    Joined:
    Nov 18, 2006
    Posts:
    85
    thanks eric but the var someString = "foo|10|20|30"; values are taken from the sql. how would i write that then >
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Hmm...not sure I understand the question....

    --Eric
     
  7. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    The var someString = "foo|10|20|30" would be replaced with the contents of what you return from your php script that queries the database.

    So you will return a heap of text from the php query, and that is what you pass through the string splitter stuff as required.