PHP/Javascript help

Discussion in 'Web Design and Development' started by MythicFrost, Jul 5, 2010.

  1. MythicFrost macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #1
    Hey,

    When my page loads I use PHP to pull the names of the tables out of a specific database, I then retrieve the values and use echo to write javascript with variable names that are in the database.

    Here is some code:
    PHP:
    echo "<script type='text/javascript'>";
    for (
    $i 0$i<count($tablesList); $i++) {
        
    //echo $tablesList[$i]." ".$i." - ";
        
    $sql "SELECT * FROM ".$tablesList[$i]." ";
        
    $result mysql_query($sql$con);
        if (
    $result) {
            echo 
    "var ".$tablesList[$i]." = new Array(".count($tablesList[$i].");";
            for (
    $k 0$k count($tablesList[$i]); $k++) {
                echo 
    $tablesList[$i]."[".$k."] = new Array();";
            }
        }
        while (
    $row mysql_fetch_array($result)) {
            echo 
    $tablesList[$i]."[".$z."][0] = '".$row["adname"]."';";
            echo 
    $tablesList[$i]."[".$z."][1] = '".$row["url"]."';";
            echo 
    $tablesList[$i]."[".$z."][2] = '".$row["linkto"]."';"//what if the order the countries are retrieved aren't the same as they are in the index.
            
    $z += 1;
        }
    }
    echo 
    "</script>";
    So, in my mind, I'm writing javascript something like this:
    table1[0][0] = "value";
    table1[0][1] = "next value"; etc.,

    Now, I have a javascript function, and I want to retrieve some information from the table1 variable (which is in javascript).

    The name of the tables are contained in a select box (drop down box) so I can easily retrieve the value of that select box, but I don't know how to use it as a variable.

    PHP:
    sometextbox.text variables(text)[0][0]
    Is this possible? Alternatively, I thought I could make a text box control and store the information in there, hide it, and then read it in javascript and then format it.
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Well, your PHP has some issues with potentially overwriting itself. Namely the if statement inside the main for loop. Also, the $z in the while loop is never initialized.

    For the JavaScript you would want something like,
    PHP:
    sometextbox.value variables[0][0];
    What you want is certainly doable. I've done similar things in the past. You just need to work through the logic of your code and give it some trial and error.
     
  3. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #3
    Thanks for your reply,
    I can't see the problem, everything there appears to be as intended.
    Its outside of both loops, although it should be set to zero before the while loop starts, thanks for picking that up.

    I'm not sure I've explained clearly enough. The name of the variable I need to access, is in a string, I need to get it out of that string.

    For example
    var randomName = "theArray";
    I need to access the variable theArray, by passing randomName as the name of the variable.

    I don't want to hard code the name of the variable, because it varies.

    So I need to do something like:
    textbox.text = allGlobalVariables[randomName][0][0];

    allGlobalVariables representing all the variables in my script, and it retrieving the one with the name randomName.

    How can I do this?
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    That would be a 3-dimensional array, but you code only produces a 2-dimensional array.

    Have you looked at the JavaScript produced by your PHP?

    Accessing global variables would be done via the window variable, window[variable].
     
  5. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #5
    Why is that a 3 dimensional array?

    window[randomName] should access the variable (in this case myArray) and then [0][0] should access the two dimensions.
     
  6. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #6
    Because there's 3 sets of [ ] there. You didn't make it clear earlier when you were writing out "variables(text)[0][0]" that variables(text) was representing something like the window object.
     
  7. MythicFrost thread starter macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #7
    Haha I was trying, didn't know what to call it though.

    EDIT: Thanks a lot for your help, I've got past that issue now.
     

Share This Page