想把下面的代码改成<select><option>的形式,但是总是调试不成功。[php]
<?php
-- 表的结构 `menu`
-- CREATE TABLE `menu` (
`id` int(11) NOT NULL auto_increment,
`fid` int(11) NOT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=30 ;--
-- 导出表中的数据 `menu`
-- INSERT INTO `menu` VALUES (26, 0, '一级分类');
INSERT INTO `menu` VALUES (27, 26, '二级分类');
INSERT INTO `menu` VALUES (28, 0, '另一个一级分类');
INSERT INTO `menu` VALUES (29, 27, '3test');
?>
[/php]
function mknodetree($nodeid=0)
{
$result=mysql_query("select * from menu where fid='".$nodeid."'"); if(mysql_num_rows($result))
{
$resultcode.= "<ul>";
while($nodeinfo=mysql_fetch_array($result))
{
$resultcode.="<li>".$nodeinfo["name"].mknodetree($nodeinfo["id"])."</li>";
}
$resultcode.="</ul>";
}
return $resultcode;
}
echo mknodetree();
[php][/php]
<?php
-- 表的结构 `menu`
-- CREATE TABLE `menu` (
`id` int(11) NOT NULL auto_increment,
`fid` int(11) NOT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=30 ;--
-- 导出表中的数据 `menu`
-- INSERT INTO `menu` VALUES (26, 0, '一级分类');
INSERT INTO `menu` VALUES (27, 26, '二级分类');
INSERT INTO `menu` VALUES (28, 0, '另一个一级分类');
INSERT INTO `menu` VALUES (29, 27, '3test');
?>
[/php]
function mknodetree($nodeid=0)
{
$result=mysql_query("select * from menu where fid='".$nodeid."'"); if(mysql_num_rows($result))
{
$resultcode.= "<ul>";
while($nodeinfo=mysql_fetch_array($result))
{
$resultcode.="<li>".$nodeinfo["name"].mknodetree($nodeinfo["id"])."</li>";
}
$resultcode.="</ul>";
}
return $resultcode;
}
echo mknodetree();
[php][/php]
<select>是不能嵌套的。
<select><option>循环出来的值</option></select>
{
echo "<option value='".$nodeinfo['id']."'>";
echo $nodeinfo["name"];
echo "</option>";
mknodetree($nodeinfo["id"]);
}
$link = mysql_connect("localhost", "root", "mysql");
mysql_select_db("test", $link);function mknodetree($nodeid=0, &$resultcode)
{
$result=mysql_query("select * from menu where fid='".$nodeid."'");
if(mysql_num_rows($result))
{
while($nodeinfo=mysql_fetch_array($result))
{
$resultcode.=" <li> ".$nodeinfo["name"]." </li>\r\n ";
mknodetree($nodeinfo["id"], $resultcode);
}
}
}
mknodetree(0, $resultcode);
echo "<ul>\r\n";
echo $resultcode;
echo "</ul>";
但是有一点啊,我只想把这个程序改成option循环。