-1

I've to do a variable array with variable name based on ID set by PHP echo.

    <script>
  var dat<?php echo $id; ?>;
  dat<?php echo $id; ?> = ["ecc..","ecc.."];
  </script>

jQuery("#name").html(dat[0]+id); // doesn't work

id is already set by a function, the question is: can i write the VAR correctly including also the PHP ID? Thanks.

Thanks

5
  • What does this have to do with Java programming? I don't see any Java in the code above. Commented Sep 4, 2016 at 20:47
  • Do you mean JavaScript? Commented Sep 4, 2016 at 20:47
  • yes javascript! Commented Sep 4, 2016 at 20:48
  • OK, I've changed your java tag to a javascript tag. Please understand that these are two completely different programming languages, about as closely related as ham is to hamburger, that if you mis-tag your question you will not get the right experts in to review it, and that this may hurt your chances of getting decent help. Commented Sep 4, 2016 at 20:50
  • 1
    Possible duplicate of How do i declare and use dynamic variables in javascript? Commented Sep 4, 2016 at 21:47

1 Answer 1

1

Try:

<script>
var dat<?php echo $id; ?>;
dat<?php echo $id; ?> = ["ecc..","ecc.."];

console.log(dat<?php echo $id; ?>[1]);
jQuery("#name").html(dat<?php echo $id; ?>[0]);
</script>

The reason it wasnt working is because dat is dat# where # is an id.

You must access an array element on dat#, not dat[pos]#.

Sign up to request clarification or add additional context in comments.

2 Comments

$id variable is in a foreach and i can't write it also in the function...
Solved, close thread, thx :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.