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

7
  • Where this code is located? Commented Jan 31, 2014 at 0:45
  • I'm sorry, what do you mean by located? Commented Jan 31, 2014 at 0:46
  • you've shown us the code. Did you get it out of your head or from some file? Commented Jan 31, 2014 at 0:47
  • it's a script that's create a tournament tree. I just need to write the team with php.. Commented Jan 31, 2014 at 0:48
  • okay, and where that script is stored? A separate .js file? Commented Jan 31, 2014 at 0:50

1 Answer 1

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

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

1 Comment

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

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.