copy 2d php array to 2d javascript array

Discussion in 'PHP' started by Hemant Agarwal, Aug 10, 2010.

  1. #1
    I have a 2d (two dimensional,2 dimensional) php array that I want to copy to a 2d javascript array.The 2d php array is $quest[100][6] and the 2d javascript array is questions[100][6] . I have written the javascript code inside the <?php....?> itself using echo "<script language="javascript" type="text/javascript">.......</script>;


    Now ,inside the javascript,when I try to copy the 2d php array to the 2d javascript array using the following method it works

    questions[0]= ["<?php echo join("\", \"", $quest[0]); ?>"];
    questions[1]= ["<?php echo join("\", \"", $quest[1]); ?>"]; ... and so on


    However, if I try to do the same using the following method it does not work
    for (var i= 0; i <= 99; i++)
    { questions= ["<?php echo join("\", \"", $quest); ?>"]; }

    Why is that?What mistake am I making?Any help will be deeply appreciated.Thanks



    -------------------------------------THE CODE----------------------------------------------
    <?php


    Access database and store result of mysq_query in $result.......
    $result = mysql_query($query);


    for ( $count = 0; $count <= 99; $count++)
    {
    $quest[$count]=mysql_fetch_array($result,MYSQL_NUM);;

    }

    echo "<script language="javascript" type="text/javascript">

    var questions = new Array(100);
    for (var i = 0; i <100; i++)
    { questions = new Array(6); }



    /*The following method of copying 2d php array to 2d javascript array is not working
    for ( var i = 0; i <= 99; i++)
    { questions= ["<?php echo join("\", \"", $quest); ?>"]; }
    */

    /*The following method ,however,is working*/
    questions[0]= ["<?php echo join("\", \"", $quest[0]); ?>"];
    questions[1] = ["<?php echo join("\", \"",$quest[1]); ?>"];
    questions[2] = ["<?php echo join("\", \"",$quest[2]); ?>"]; ....and so on




    </script>";


    mysql_close($db_server);

    ?>
     
    Hemant Agarwal, Aug 10, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    I don't know about your code above, but you should be using single quotes, so change your echo from

    
    echo "<script language="javascript" type="text/javascript">
    //and this
    </script>";
    
    PHP:
    to this
    
    echo [COLOR="Red"][B]'[/B][/COLOR]<script language="javascript" type="text/javascript">
    //and this
    </script>[COLOR="Red"][B]'[/B][/COLOR];
    Code (markup):
     
    MyVodaFone, Aug 10, 2010 IP
  3. Hemant Agarwal

    Hemant Agarwal Member

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #3
    Hi,
    In case,it is difficult to figure out the mistake with my code,can someone tell me,any other method to copy a 2d php array to a 2d javascript array.Thanks
     
    Hemant Agarwal, Aug 10, 2010 IP
  4. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #4
    Here's my approach:
    
    <?php
    
    // view in plain text
    header("Content-Type: text/plain");
    
    // create a 2d array with random numbers in it
    for($i = 0; $i < 10; $i++) {
       for($j = 0; $j < 10; $j++)
       $arr[$i][$j] = rand( );
    }
    
    
    // write js
    $js_arr = "<script type=\"text/javascript\">\nvar arr = new Array( );\n";
    
    for($i = 0; $i < 10; $i++) {
       // write a 2d js array declaration
       $js_arr .= "arr[$i] = new Array( );\n";
       for($j = 0; $j < 10; $j++)
       // write a js array
       $js_arr .= "arr[$i][$j] = \"" . $arr[$i][$j] . "\";\n"; // this is the general idea
    }
    $js_arr .= "</script>";
    
    echo $js_arr;
    ?>
    
    PHP:
     
    Last edited: Aug 10, 2010
    Rainulf, Aug 10, 2010 IP
  5. AustinQPT

    AustinQPT Member

    Messages:
    75
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #5
    Rainulf your approach was quite nice.

    here is how I would go about it:

    Simply change $array to the variable of the array.
    
    <?
    header("Content-Type: text/plain");
    if($array == null){
    echo'<script type="text/javascript">
    var arr = new Array( );
    ';
    }
    //some stuff will need to be changed
    $data = $array; //change array to the variable of the array
    //now we find array size
    
    $amt = count($data);
    $amt2 = count($data[0]);
    $amt3 = count($data[1]);
    $i = 0;
    $k = 0;
    while($i < $amt) {
    $j = 0;
       while($j < $amt2){
       echo"arr[".$i."][".$j."]=\"".$data[$i][$j]."\";\n";
       $j++;
       }
    $i++;
    }
    ?>
    </script>
    
    PHP:
     
    AustinQPT, Aug 13, 2010 IP