0

EDIT: I've tried to implemented the solution, but it still doesn't work. Sorry i'm a programming idiot. Thanks for the link to the page.

In my first function databaseBOscreens i am attempting to obtain symbols of stocks according to the filter selected for a broker and/or an asset class. The function sends a request to another file which performs SELECT statement to lookup for the relevant symbols using mysqli. The result would be an array of symbols, which i then encode and subsequently parsed in this js function.

Each symbol will then go through a second process, which is my second function generatePrice() to obtain the historical prices for that particular symbol. What i'm trying to do is to loop through all symbols to generate and store the priceArr as an array in the resultArr. Sorry for being unclear.

function databaseBOscreens(broker,assetClass) {
var xhr;  
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();  
    } 
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");  
    }
    var data = "broker=" + broker + "&category=" + assetClass;  
    xhr.open("POST", "generateSymbol.php", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send(data);
    xhr.onreadystatechange = receive_data;
    function receive_data() {       
        if (xhr.readyState == 4 && xhr.status == 200) 
        {  
        var resultArr = JSON.parse(xhr.responseText);
        var priceArr = new Array();
    for(var ctr=0;ctr<resultArr.length;ctr++)
    {
        generatePrice(function(result,resultArr[ctr].symbol)){
                priceArr[ctr] = result;
                }
    }
        }
    }
}

function generatePrice(callback,symbol) {
    var xhr2;  
    if (window.XMLHttpRequest) {
        xhr2 = new XMLHttpRequest();  
    } 
    else if (window.ActiveXObject) {
        xhr2 = new ActiveXObject("Microsoft.XMLHTTP");  
    }
    var data2 = "symbol=" + symbol;  
        xhr2.open("POST", "generatePrice.php", true);
        xhr2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr2.send(data2);

        xhr2.onreadystatechange = receive_data2;
        function receive_data2() {
            if (xhr2.readyState === 4)
        {
            if (xhr2.status === 200) 
            {  
                var priceArr = JSON.parse(xhr2.responseText);
                callback(priceArr);
            }
            }
        }
 }



function receive_data() {       
    if (xhr.readyState == 4 && xhr.status == 200) 
    {  
        var resultArr = JSON.parse(xhr.responseText);
        var priceArray = new Array();
        for(var ctr=0;ctr<resultArr.length;ctr++)
        {
                priceArray = generatePrice(resultArr[ctr].symbol);      
        }
        alert(priceArray.length);
    }       
}

My function generatePrice() returns an array, but i can't seem to store it in another array to create a multidimensional array. I've search everywhere i can't seem to get it work. Thanks in advance for any help rendered

generatePrice function:

function generatePrice(symbol) {
var xhr2;  
 if (window.XMLHttpRequest) {
    xhr2 = new XMLHttpRequest();  
} else if (window.ActiveXObject) {
    xhr2 = new ActiveXObject("Microsoft.XMLHTTP");  
}
var data2 = "symbol=" + symbol;  
    xhr2.open("POST", "generatePrice.php", true);
    xhr2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr2.send(data2);

    xhr2.onreadystatechange = receive_data2;
    function receive_data2() {
        if (xhr2.readyState == 4 && xhr2.status == 200) 
        {  
            var priceArr = JSON.parse(xhr2.responseText);
            return priceArr;
        }
    }
}
Soggeh
  • 101
  • 1
  • 8
  • Your function is **asynchronous**, and can't return a value that it hasn't fetched yet. Read this for a better explanation, and several good solutions: Possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – user229044 Aug 13 '13 at 13:38
  • Your edit is unfortunate. Your first paragraph should describe your problem briefly, instead of being fluff followed by a code dump without context. – millimoose Aug 13 '13 at 15:26
  • (Aside: Javascript sorely, sorely needs its async/await clone.) – millimoose Aug 13 '13 at 15:27

1 Answers1

0

My function generatePrice() returns an array,

No it doesn't, it returns undefined. XHR is asynchronous, see How do I return the response from an asynchronous call?.

priceArray = generatePrice(resultArr[ctr].symbol);

I can't seem to store it in another array

You didn't attempt to do so, you just stored the result of every call in the priceArray variable - instead of in a property of the array. Use

priceArray[ctr] = generatePrice(resultArr[ctr].symbol);
// or
priceArray.push( generatePrice(resultArr[ctr].symbol) );
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375