1

I know this kind of post is frequently found on internet. But my problem is a little bit more dificult and I did not find an answer.

I want to make an associative array in Javascript in a loop with a variable name. ($JJ = table of object) ($JJ->getResto($DB,$acad) allows me to recover my database datas)

$JJ = new RestaU();  
$JJ = $JJ->getResto($DB,$acad);
    for ($i = 0; $i <= sizeof($JJ)-1; $i++) {
        //Datas recovering
        $lat = $JJ[$i]->loc_lat;
        $long = $JJ[$i]->loc_long;
        $name = $JJ[$i]->nom;
        $ville = $JJ[$i]->ville;

        //String treatment to avoid spaces
        $ville = str_replace(" ","",$ville);
        $name = str_replace(" ","",$name);

echo <<< SC
    <script>
            //here $ville will contain an google map object associated to the ville name.
            //It means that I need to change it for each "for" loop.

            var $ville = new Object();

            //that is why here I need to have $ville["name"] to generate a table for each city 
            //and have an access to it later.
            $ville+["name"] = new google.maps.LatLng($lat,$long);
            console.log(string2);
    </script>
SC;

My problem is, I can not find the solution to right for example ville1["name"]. Each time the code is not "interpreted" it means I can have the string but it does not create my array.

Thank you a lot for all your ideas!

SOLUTION IN COMMENT. I used that : var string = "$ville"; window[string]["name"] = "blabla";

It works really well.

glihm
  • 1,138
  • 13
  • 29
  • possible duplicate of [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – dynamic Nov 11 '14 at 21:12
  • 1
    Not the duplicate if you read the whole topic :) – Martijn Nov 11 '14 at 21:14
  • And the problem is that your JS can't read PHP variables? or...? – vcanales Nov 11 '14 at 21:17
  • @Martijn really sorry I did a duplicate? I did not read the answer to it.. I will read the topic one more time. Thanks – glihm Nov 11 '14 at 21:17
  • @devJunk : I can not find how I can create the tables with a name which can change. Because I need to use table["attribute"], when I concatenate I do not have tables made. (It is difficult to be clear, sorry). – glihm Nov 11 '14 at 21:19
  • You want the name of the JS Variable to be generated by PHP? – vcanales Nov 11 '14 at 21:25
  • What is the "+" in $ville+['name'] ? – vcanales Nov 11 '14 at 21:27
  • I find the solution : var $acad = new Object(); var string1 = "$acad"; window[string1]["$name"] = new google.maps.LatLng($lat,$long); It works perfectly. I will generate an array with a name that I can change in a loop. Thank you all for your time spent on it. – glihm Nov 11 '14 at 22:28
  • @devJunk : The + was to explain that I want to concatenate the string of $ville with ["name"] to form ville["name"] usable to make an associative array. I did it : string1 = "$ville". Then window[string]["name"] and it works perfectly. – glihm Nov 11 '14 at 22:45

1 Answers1

1

php is server language and javascript is clint browser language!
in fact you can't share variables!
But you can print variables to javascript code like:

<script>
  var myvar = '<?php echo $myvar; ?>';
</script>

if you want to send variables from javascript to php you must use Ajax
for array

<script>
    <?php
    $array = array('index1'=>'hellow World!','index2'=>'Iran haven`t nuclear bomb');
    echo 'var arryname = new Array();';
    foreach($array as $key=>$val){
      echo "\n arryname['{$key}'] = '{$val}';";
    }
    ?>
</script>
Mahmoud.Eskandari
  • 1,460
  • 3
  • 20
  • 32
  • Hi, thank you for your answer @M.Eskandari. // If I do : var myvar = ''; Can I use later : myvar["att.1"]? It will work? I try. – glihm Nov 11 '14 at 21:21
  • You should print all indexes in javascript! i'll edit Answer and add code for print array from php to JS! – Mahmoud.Eskandari Nov 11 '14 at 21:24
  • No, it does not word. When I try to do console.log(myvar["att.1"]) I have "undefined" in the console. It means the table is not created I think. – glihm Nov 11 '14 at 21:26
  • Thank you a lot for your time. I am on it to try to understand this. – glihm Nov 11 '14 at 21:27
  • I am trying it and sure I will do accept if it works! – glihm Nov 11 '14 at 21:39
  • I do not know why my php code is not interpreted. I have exactly the same code written in the console... Normaly I am supposed to have the RESTULT of this code, no @M.Eskandari? – glihm Nov 11 '14 at 21:46
  • ` – Mahmoud.Eskandari Nov 11 '14 at 21:49
  • Yes, we are agree about this point. But I still not have the code executed. For instance, I write into my balises and I do not have the variable printed. – glihm Nov 11 '14 at 22:02