Insert MySQL data into JavaScript with PHP

Hello
I found this really nice JS Tree script,
http://www.destroydrop.com/javascripts/tree/,
and I want to populate the tree with data from
MySQL but can’t get it to work.

I tried ob_start() without any luck
and a wrapper:


test.php:
<html>
<body bgcolor=“#FFFFFF”>
<h1>JS Tree</h1>
<script language=“JavaScript” type=“text/javascript” src=“js.php”></script>
</body>
</html>


js.php:
<?php

header(“Content-Type: text/javascript”);


connect to mysql
make query

?>

  document.write('JS Tree');      //Working


  d = new dTree('d');

  d.add(0,-1,'My example tree');
  d.add(1,0,'&lt;?php echo $mysqlfield;?&gt;','example01.html');
  d.add(2,0,'Node 2','example01.html');

  document.write(d);

but it don’t work. It doesn’t output the tree at all,
only document.write(‘JS Tree’);

Anybody?

Try to just print out variables containing results from MySQL query, that will help you to get one step closer to finding the problem.

All you need to do is echo the MySQL data into your JS code at the relevant points. PHP runs on the server and the output is (mainly) just text data as far as PHP is concerned - it doesn’t know or care whether this data is HTML tags, JavaScript code, plain text.

UNTESTED!

<script type="text/javascript">
d = new dTree ('d');
d.add (0,-1,'My example tree');

<?php
// Connect to MySQL and get data, then
while ( $row = mysql_fetch_array ($rs) ) {
    $p1 = $row['Index'];
    $p2 = $row['Level'];
    $p3 = $row['URL'];
    echo "d.add ($p1,$p2,'$p3');" . "\
";
}
?>

document.write (d)
</script>

kool

So simple.
Thanx