A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: problem with array

  1. #1

    problem with array

    i'm trying to create an array from the datas i'm getting with loadvariablesnum with my php file.
    as i saw that the synthax to create an array was like new Array("data1","data2",etc), i've returned a line of result that look like this:

    "bb","boubou","bb","kalou"

    so if i name a textfield "connected" (which is the name of this variable), i have this string writen in it.

    pb: if i make a :
    connected2=new Array(connected);
    ok i can see my array in my textfiel called connected2

    and then i try to sort my values with:
    connected4=connected2[0];

    it returns nothing...someone may help about setting a flash array from values loaded from php (as it seems to be my problem)

  2. #2
    Senior Member
    Join Date
    Feb 2002
    Posts
    536

    Re: problem with array

    An easy way to build your array:

    code:

    txt = "a,b,c,d";
    yourArray = txt.split(",");
    trace (yourArray.length);
    for (i in yourArray) trace (yourArray[i])



    Now...

    If u retrieving some data from php script and having it displayed like "bb","boubou","bb","kalou" u could try:

    code:

    someVar="bb","boubou","bb","kalou"; // from php script

    newArray=someVar.split('"').join("").split(",");

    trace (newArray.length)
    for (i in newArray) trace (newArray[i]);



    as i have no idea how are your script printing those data, I can only 'guess' the best way to do it.

    If your php script doesn't return a variable and just return some data use LoadVars instead:

    code:

    var tmp = new LoadVars();
    tmp.load ("yourFile.php");
    tmp.onData = function ($d) {
    newArray = $d.split('"').join("").split(",");
    trace (newArray.length);
    for (i in newArray) trace (newArray[i]);
    }


  3. #3

    still pb with array

    ok,so thx for your answer, but after many tryes i still have the same pb: impossible to built this ****ing array!!!

    here is, again my latest code:

    //load my datas into a variable called "connected

    loadVariablesNum ("connected_flash.php?" + randomnumber(), 0, "POST");

    //supposed to build the array from the variable
    var connectedx = [connected];

    //at this point, my textfield called "connectedx" returns a line that look like this: bb,boubou,kalou,mat,charp. So i've got datas

    //supposed to define my array
    connected3 = connectedx.split(",");

    and impossible to retrieve any connected3[0] datas...the same way if i make:
    varlenght=connected3.lenght;//returns nothing

    i'm becoming crazy with this...

  4. #4
    Senior Member
    Join Date
    Feb 2002
    Posts
    536
    what is the output from the php script?


    code:


    var tmp = new LoadVars();
    tmp.sendAndLoad ("connected_flash.php?" + randomnumber(), tmp, "POST");
    tmp.onLoad = function (ok) {
    if (ok) {
    connectedx = this.connected;
    connected3 = connectedx.split(",");
    trace (connected3.length);
    }
    }




    I supposed your problem is the loadVariablesNum coz u'r running all commands at once. If u use loadVariables u have to use a function (such as setInterval or getTimer) to check when the data get loaded and after that build your array.

    Something like:

    code:

    loadVariablesNum ("connected_flash.php?" + randomnumber(),0, "POST");

    checking = setInterval(function () {
    if (connected != undefined || connected != "") {
    connectedx = connected;
    connected3 = connectedx.split(",");
    trace (connected3.length);
    clearInterval(checking);
    }
    },100);



    But as u said yourself, u already got datas...

    //supposed to build the array from the variable
    var connectedx = [connected];
    try just

    var connectedx = connected;


    or u can zip all the files (php and fla) and post it here. I'll take a look.

  5. #5

    pb with array

    hello

    i've finally succeed in creating my buttons with this array, and i can see my datas with trace. know if you can give me a clue:

    here is my code:

    randomNumber = Math.random(100000);
    loadVariablesNum("http://localhost/LOVINYOO/chat/connected_flash.php?" + randomnumber(), 0, "POST");
    var connected2=connected.toString();
    data = connected2.split(",");
    for (n=0; n<data.length; n++) {
    _root.attachMovie("myButton", "myButton"+n, n);
    setProperty("myButton"+n, _x, 380);
    setProperty("myButton"+n, _y, 20*n);
    _root["myButton"+n].myButtonTextField.text = data[n];
    trace(n);
    trace(data[n]);
    }

    so this duplicate my button as many times as i have a data line.My proiblem is that i don't manage to put the text on each button, with the line _root["myButton"+n].myButtonTextField.text = data[n];
    my trace give me the values of each data[],so these values are present...may you help..? thx

  6. #6
    Senior Member
    Join Date
    Feb 2002
    Posts
    536
    your code seems to be ok...

    try to convert buttons into movieclips.

    And remember, when working online, loadVariablesNum won't work properly as it should.

  7. #7

    pb with array

    thx again for your answer. the point is that my button is already a movie clip (the movie myButton is a MC containing the button)...have some more clue???

    and thx for the details about loadvariablesnum..i didn't undersatand why it took so much time to load the datas (in general i had to wait a full movie loop to have them loaded)...whta would u use better than loadvariablesnum..?

  8. #8
    Senior Member
    Join Date
    Feb 2002
    Posts
    536
    I used to use loadVars.


    code:


    var lv = new LoadVars();
    lv.sendAndLoad ("yourUrl.php",lv,"POST");
    lv.onLoad = function (ok) {
    if (ok) {
    // your script to handle data here
    }
    }




    I've also attached an example of duplicateMovieClip, without loading external data. U can change it to load the php script.
    Attached Files Attached Files

  9. #9

    pb with array

    hello again...thx for your help, this is working (almost) perfectly...
    now i've got some other questions:

    the aim of all this is the creation of a private chat with php...
    so the query that retrieve the datas looks like this:
    $query=mysql_query("SELECT * FROM chat_messages WHERE expediteur='$exp' AND dest='$dest' OR expediteur='$dest' AND dest='$exp' ORDER BY date DESC");

    where "$exp" is me, and "$dest" is the person i'm talking to...so i'm calling this page this way in flash:

    loadVariablesNum("http://localhost/chat/messages.php? + randomnumber(), 0, "POST");

    basically, i've send the exp and dest variables in the html page where i'm calling the swf movie...what i would like to do is to use the buttons we've created together to send the dest variable to the line i'm calling the php script, to retrieve the messages coming from this person (and additionnaly to switch my conversation screen to this person conversation)
    so is it possible to send this variable like this for example:

    loadVariablesNum("http://localhost/chat/messages.php?dest="+$data[$i] + randomnumber(), 0, "POST");

    or what is the way to do so..?

    another, personal question: where you coding php before using flash..? as the way you're handling variables in flash (with "$") is closer to php than what i saw before in flash...

  10. #10
    Senior Member
    Join Date
    Feb 2002
    Posts
    536
    if your php script retrieves the '$dest' using GET u can use

    loadVariablesNum("http://localhost/chat/messages.php?dest="+$data[$i] + randomnumber(), 0, "POST");


    but if your php uses POST then u'll have to set

    dest = $data[$i];
    loadVariablesNum("http://localhost/chat/messages.php? + randomnumber(), 0, "POST");



    another, personal question: where you coding php before using flash..? as the way you're handling variables in flash (with "$") is closer to php than what i saw before in flash...
    In fact I learned actionscript first. I started to learn PHP a couple months ago and then I saw how php handles variables (using '$'). But using '$' in my codes help me to identify variables, what makes the process to find some bug or error much more easy.

  11. #11

    pb with array

    ok, but how to transmit the value of each returned line to my button MC..? in the script u sent me, i tryed to do the following, on the onclick frame of the button:

    loadVariablesNum("http://localhost/LOVINYOO/chat/messages.php?dest="+$data[$i] + randomnumber(), 0, "POST");

    but at this moment the variable $data[$i] is undefined...
    i also tryed this:

    var $dest=$data[$i];
    loadVariablesNum("http://localhost/LOVINYOO/chat/messages.php?dest="+$dest + randomnumber(), 0, "POST");

    and again the $dest variable is undefined...

  12. #12
    Senior Member
    Join Date
    Feb 2002
    Posts
    536



    check this fla.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center