0

Using php I printed an <script> tag and some JavaScript lines. It worked as it should have. Script I'm printing includes generate some objects from a library I'm using. However, when I want do that according to some condition, in this case, a for cycle I can't get it working.

This is the code I tried first:

<?php
$teacher1 = "teacher1";
echo "
<canvas id=\"lienzoGrafo\" width=\"800\" height=\"600\"></canvas>

    <script languaje=\"javascript\" type=\"text/javascript\" charset=\"UTF-8\">

    var grafo = arbor.ParticleSystem({repulsion: 3000, friction:.1, stiffnes:900, gravity:true});
    grafo.renderer = Renderer('#lienzoGrafo')
    var teacher1 = grafo.addNode(\"$teacher1\",{color:'blue',width:100, shape:'dot',label:\"$teacher1\"})

As can be seen in last line of code I'm actually using a php variable for completing the script printing.

However, when I try to use a cycle to complete the cycle I can't get it working.

This is what I've tried:

<?php
$teacher1 = "teacher1";

echo " 
    <canvas id=\"lienzoGrafo\" width=\"800\" height=\"600\"></canvas>

    <script languaje=\"javascript\" type=\"text/javascript\" charset=\"UTF-8\">
    var grafo = arbor.ParticleSystem({repulsion: 3000, friction:.1, stiffnes:900, gravity:true});
    grafo.renderer = Renderer(\"#lienzoGrafo\")";
    for ($i=0; $i < 5; $i++) { 
        echo "grafo.addNode(\"teacher\".$i,{color:'blue',width:100, shape:'dot',label:\"teacher\".$i})";
    }

    echo "</script>";

?>

But I can't get anything working, browser console outputs:

Uncaught SyntaxError: Unexpected identifier localhost/:19

But my index.php has the </html> tag at line 19.

I think problem is that html script statements are being rendered all together

grafo.renderer = Renderer("#lienzoGrafo")grafo.addNode("teacher0",{color:'blue',width:100, shape:'dot',label:"teacher0"})grafo.addNode("teacher1",{color:'blue',width:100, shape:'dot',label:"teacher1"})grafo.addNode("teacher2",{color:'blue',width:100, shape:'dot',label:"teacher2"})grafo.addNode("teacher3",{color:'blue',width:100, shape:'dot',label:"teacher3"})grafo.addNode("teacher4",{color:'blue',width:100, shape:'dot',label:"teacher4"})</script>
4
  • Why not round off each segment of code with the closing ?> tag, then just put your script as normal. Commented Nov 26, 2013 at 15:01
  • 1
    languaje may have something to do with it. Also it looks like the only php there is the loop. I'd just do the php there versus echoing the whole script Commented Nov 26, 2013 at 15:03
  • You really should look into using a HEREDOC to do multiline echo/assignments. That or break out of PHP mode and simply stick with html mode. Either will save you from having to escape all the quotes in the text. Commented Nov 26, 2013 at 15:16
  • You could suggest an example as answer Commented Nov 26, 2013 at 15:17

3 Answers 3

1

Change this line in your for loop ...

echo "grafo.addNode(\"teacher" . $i . "\",{color:'blue',width:100, shape:'dot',label:\"teacher" . $i . "\"})";
Sign up to request clarification or add additional context in comments.

Comments

1
echo "grafo.addNode(\"teacher$i\",{color:'blue' etc...
                             ^^---note the position

As written, your PHP code is generating

grafo.addNode("teacher"7,{color:etc....
                       ^---syntax error

2 Comments

I changed it to echo "grafo.addNode(\"teacher" . $i . "\",{color:'blue',width:100, shape:'dot',label:\"teacher" . $i . "\"})"; as other asnwer suggested but I got not results. Check my edit, I think the reason of it is how html is being rendered.
then do a view-source on your page and look at the actual output of your server-side code. don't debug in the dark. you'll see exactly where the syntax errors are.
0

As I realized the rendered html had JavaScripts statements all together, I just added a semicolon (;) to JavaScript statements.

grafo.renderer = Renderer(\"#lienzoGrafo\");";

and

echo "grafo.addNode(\"teacher" . $i . "\",{color:'blue',width:50, shape:'dot',label:\"teacher" . $i . "\"});";

did the trick!

My learning

  • Always look for the html php generates.
  • It's not always a good idea to end a Script statement without semicolon.
  • Only use php when it's precisely necessary.

Comments

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.