我试图用数据库动态输出菜单树。代码似乎没有记录JavaScript函数或正确编写。有人有想法吗?
<? php
echo "<script type='text/javascript' src='dtree.js'></script>";
$db = new MyDB();
$sql =<<<EOF
SELECT DISTINCT CATEGORY FROM ITEM;
EOF;
$ret = $db->query($sql);
$i = 1;
echo "<script type='text/javascript'>";
echo "d = new dTree('d');";
echo "d.add(0,-1,'Introduction', 'index.html','','','img/blue.gif');";
while($row = $ret->fetchArray(SQLITE3_ASSOC)){
echo "d.add( " . $i . ", 0, '" . $row['CATEGORY'] . "', );";
$i++;
}
echo "document.write(d);";
echo "</script>";
$db->close();
echo "Operation done successfully\n";
?>
答案 0 :(得分:0)
在你的while循环中,你省略了dTree.add
按钮的最后一个参数,检查while循环中的第一行:
while($row = $ret->fetchArray(SQLITE3_ASSOC)){
echo "d.add( " . $i . ", 0, '" . $row['CATEGORY'] . "', );";
$i++;
}
您应该删除逗号(,
),或添加参数值。所以下面两个例子都是正确的。
echo "d.add( " . $i . ", 0, '" . $row['CATEGORY'] . "', '');";
echo "d.add( " . $i . ", 0, '" . $row['CATEGORY'] . "');";
为了使它更清楚,下面的第一行是你的javascript现在的样子,另外两行是应该输出的有效javascript的例子。
d.add(1, 0, 'A string', ); // NOT correct, see the last comma
d.add(1, 0, 'A string', ''); // correct, by adding an empty string, BUT LOOK IN THE DOCS FOR THE RIGHT PARAMETER TYPE
d.add(1, 0, 'A string'); // Also correct, no fourth parameter