-1

I am using a getJSON method to post the data I have in a database, through a for loop and into an HTML page. But I would like to the function to call different tables in my database depending on the integer the for loop is currently on, something like this:

for (var r = 0; r < 8; r++){
     $.getJSON("PHP-PAGE.php?jsoncallback=?", function(table+r) {
          //function stuff here
     });
}

But when I try to do this, the "table+r" is flagging a syntax error. What am I doing wrong?

Murphy1976
  • 1,415
  • 8
  • 40
  • 88

4 Answers4

2

You are defining a function, not calling it. Between ( and ) you have to put identifiers (variable names) not expressions.

To pass data here, you need to use variables from a wider scope than the function. Since the variable is going to change (and the function is called asynchronously) you have to use a closure to do this.

function mkCallback(table) {
    var foo = "table" + table;
    return function () {
        // function stuff that uses foo here
        // foo from the time mkCallback was called to make this function
        //    will still be in scope
    };
}

for (var r = 0; r < 8; r++){
     $.getJSON("PHP-PAGE.php?jsoncallback=?", mkCallback(table+r));
}
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

I think what you probably want is

for (var r = 0; r < 8; r++){  //outer loop

     function(tablenum){  //closure function
         tablename = table+tablenum // saved reference to "table+r"
         $.getJSON("PHP-PAGE.php?jsoncallback=?", function() {
           //function stuff here, using tablename as the param
         });
     }(r)
}

This creates a closure to maintain the value of the iterated value. You can reference tablename in the callback function, and that will refer to a value equivalent to table+r

The issues with your original example

  1. You were putting table+r as a parameter to a function you were defining, rather than an argument to one you were calling
  2. You were trying to get the callback to reference r. But the callback won't run until after the loop has executed, so r will be 8 for all callback functions.
  3. If you were trying to reference "table1", "table2" then you want to have "table"+r. Otherwise I assume you're referencing a table variable outside the scope of the code you showed us.
Ben McCormick
  • 25,260
  • 12
  • 52
  • 71
0

function(table+r) { tries to create a function with table+r as a parameter, but + is not valid in a variable name. I think you instead want something like this:

for (var r = 0; r < 8; r++){
     $.getJSON("PHP-PAGE.php?jsoncallback=?", 
     (function(currentR){
         return function() {
               var someVariable=table+currentR; // No idea where table came from...
               //function stuff here
         }
     })(r));
}

As @Quentin mentioned by the time the callback is called, r will have reached its final value, hence the interesting closure.

Community
  • 1
  • 1
Joshua Dwire
  • 5,415
  • 5
  • 29
  • 50
  • That won't work. By the time the first HTTP response arrives, `r` will have reached its final value. – Quentin Mar 05 '13 at 17:23
-1

You can directly reference the variable r in your callback. Not sure what table is - the return data from the JSON call? Try the following:

for (var r = 0; r < 8; r++){
     $.getJSON("PHP-PAGE.php?jsoncallback=?", function(jsonReturnData) {
          //function stuff here
          alert(r);
     });
}
Andy Magee
  • 226
  • 3
  • 10