diegoaguilar
diegoaguilar

Reputation: 8376

How to print html script dynamically with php?

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>

Upvotes: 0

Views: 137

Answers (3)

diegoaguilar
diegoaguilar

Reputation: 8376

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.

Upvotes: 0

Marc B
Marc B

Reputation: 360632

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

Upvotes: 1

cmorrissey
cmorrissey

Reputation: 8583

Change this line in your for loop ...

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

Upvotes: 1

Related Questions