0

The headline is properly pretty bad, but i don't know what to name it.. Anyways: I have this simple problem with a javascript code that i want to display some php code, but i don't know how to do it.. Here is the code:

   var bigData = {
"teams" : [],
"results" : [] };

for( var i=1 ; i<16 ; i+=2 )
   { 
    bigData.teams.push(["<?php echo 1; ?>",'Team '+(i+1)]);
}

for( var j=1 ; j<16 ; j++ ) {
  bigData.results.push([1,2]);
}

As you see im trying to just print something in php inside the javascript but i can't. Any one who got a solution?

Best regards Emil

Emil
  • 21
  • 4

1 Answers1

2

add this tag on your html file

<script type="text/javascript" src="/javascript.php"></script>

in your javascript.php file:

<?php
header("Content-Type:text/javascript");
$arr = array();
echo 'var array_data = [';
for( $i = 0; $i < 10; $i++ ){
  $arr[] = "[{$i},'Team {$i}']";
};
echo implode(",",$arr);
echo ']';

then in your js file you will have a js array called array_data then you can iterate over it

markcial
  • 9,041
  • 4
  • 31
  • 41
  • will be much better a json service called over rest verbs with some cache, but it will need a more complex explanation, just to give some guidelines the solution fits – markcial Jan 31 '14 at 00:54