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>
?>tag, then just put your script as normal.languajemay 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