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.

9
  • possible duplicate of How to pass variables and data from PHP to JavaScript? Commented Nov 11, 2014 at 21:12
  • 1
    Not the duplicate if you read the whole topic :) Commented Nov 11, 2014 at 21:14
  • And the problem is that your JS can't read PHP variables? or...? Commented Nov 11, 2014 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 Commented Nov 11, 2014 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). Commented Nov 11, 2014 at 21:19

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

8 Comments

Hi, thank you for your answer @M.Eskandari. // If I do : var myvar = '<?php echo $myvar; ?>'; Can I use later : myvar["att.1"]? It will work? I try.
You should print all indexes in javascript! i'll edit Answer and add code for print array from php to JS!
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.
Thank you a lot for your time. I am on it to try to understand this.
I am trying it and sure I will do accept if it works!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.