帮帮忙,帮我看看我下面的代码错在那里了。
刀仔
![]()
|
1#
刀仔 发表于2007-05-24
帮帮忙,帮我看看我下面的代码错在那里了。
下面是我要参照网上面的例子写的树状菜单的数据库和代码
MYSQL -- phpMyAdmin SQL Dump -- version 2.10.1 -- http://www.phpmyadmin.net -- -- 主机: localhost -- 生成日期: 2007 年 05 月 24 日 12:49 -- 服务器版本: 5.0.37 -- PHP 版本: 5.2.1 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; -- -- 数据库: `test` -- -- -------------------------------------------------------- -- -- 表的结构 `tree2` -- CREATE TABLE `tree2` ( `id` tinyint(3) unsigned NOT NULL auto_increment, `p_id1` tinyint(2) NOT NULL default '0', `p_id2` tinyint(2) default NULL, `p_id3` tinyint(2) default NULL, `p_id4` tinyint(2) default NULL, `topic` varchar(50) default NULL, `url` varchar(60) NOT NULL default '', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=29 ; -- -- 导出表中的数据 `tree2` -- INSERT INTO `tree2` (`id`, `p_id1`, `p_id2`, `p_id3`, `p_id4`, `topic`, `url`) VALUES (13, 1, 0, 0, 0, '卷绕机', ''), (14, 1, 1, 0, 0, 'SBW-800', ''), (15, 1, 1, 1, 0, 'A部件', ''), (16, 1, 1, 2, 0, 'B部件', ''), (17, 1, 1, 1, 1, 'SBW-A-001', ''), (18, 1, 1, 1, 2, 'SBW-A-002', ''), (19, 2, 0, 0, 0, '电阻机', 'HTTP://KDKDDK'), (20, 2, 1, 1, 0, 'SRS-A部件', ''), (21, 2, 1, 2, 0, 'SRS-B部件', ''), (22, 2, 1, 1, 1, 'SRS-A-001', 'HTTP://DFDFD'), (23, 2, 1, 1, 2, 'SRS-A-002', 'HTTP://JDJDJD'), (24, 2, 1, 2, 1, 'SRS-B-001', 'HTTP:FDFDF'), (25, 2, 1, 2, 2, 'SRS-B-002', 'HTTP://DFDFJ'), (26, 2, 1, 0, 0, 'SRS-700', ''); 上面是数据库里面的东西,需要说明一下树状菜单只有四级P_id1是第1级,p_id2是第2级,p_id3是3第级,p_id4是第4级, <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <style type="text/css"> <!-- body { background-color: #00CCFF; } --> </style></head> <link href='../style.css' rel=stylesheet> <script LANGUAGE="JavaScript1.2"> function ShowMenu(MenuID) { if(MenuID.style.display=="none") { MenuID.style.display=""; } else { MenuID.style.display="none"; } } </script> <body> <?php //基本变量设置 $GLOBALS["ID"] =1; //用来跟踪下拉菜单的ID号 //连接数据库 $Con=mysql_connect("localhost","root","root"); mysql_select_db("test"); $sql_1="select * from tree2 where p_id2=0 and p_id3=0 and p_id4=0"; //提取一级菜单 mysql_query("set names gbk"); $result_1=mysql_query($sql_1,$Con); //如果一级菜单存在则开始菜单的显示 if(mysql_num_rows($result_1)>0) { $numr_1=mysql_num_rows($result_1); echo "<table cellpadding='0' cellspacing='0' border='0'>"; for($rows_1=0;$rows_1<$numr_1;$rows_1++) { $menu_1=mysql_fetch_array($result_1); //将第一级菜单项目的内容导入数组 $url_1=$menu_1['url']; $name_1=$menu_1['topic']; echo "<tr>";//======================= $sql_2="SELECT * FROM tree2 WHERE p_id1 =$menu_1[p_id1] AND p_id3 = 0 AND p_id4 = 0 AND p_id2 != 0"; $result_sub_2=mysql_query($sql_2); if(mysql_num_rows($result_sub_2)>0) { echo "<td width='20'><img src='foldericon.png' border='0'></td>"; echo "<td class='Menu' onClick='javascript:ShowMenu(Menu".$GLOBALS["ID"].");'>"; } else { echo "<td width='20'><img src='file.png' border='0'></td>"; echo "<td class='Menu'>"; } if($url_1!="") { echo "<a href='$url_1' target=mainFrame >[".$menu_1[p_id1]."]$name_1</a>"; } else { echo "<a>[".$menu_1[p_id1]."]$name_1</a>"; } echo "</td></tr> ";//======================= if(mysql_num_rows($result_sub_2)>0) { //指定该子菜单的ID和style,以便和onClick语句相对应 echo "<tr id=Menu".$GLOBALS["ID"]++." style='display:none'>"; echo "<td width='20'> </td>"; echo "<td>"; //echo "</td></tr>"; } $numr_2=mysql_num_rows($result_sub_2);//获取$sql_1的记录笔数 if($numr_2>0) { echo "<table cellpadding='0' cellspacing='0' border='0'>"; //=======================第二级的表头代码 for($rows_2=0;$rows_2<$numr_2;$rows_2++) { //echo "<table cellpadding='0' cellspacing='0' border='0'>"; $menu_2=mysql_fetch_array($result_sub_2);//将第二级菜单目录导入数组 $url_2=$menu_2['url']; $name_2=$menu_2['topic']; echo "<tr>"; //======================= //提取第三层子菜单记录集 $sql_3="SELECT * FROM tree2 WHERE p_id1 =$menu_2[p_id1] AND p_id2 = $menu_2[p_id2] AND p_id4 = 0 AND p_id3 != 0"; $result_sub_3=mysql_query($sql_3); if(mysql_num_rows($result_sub_3)>0) { echo "<td width='20'><img src='foldericon.png' border='0'></td>"; echo "<td class='Menu' onClick='javascript:ShowMenu(Menu".$GLOBALS["ID"].");'>"; } //if(mysql_num_rows($result_sub_3)>0) else { echo "<td width='20'><img src='file.png' border='0'></td>"; echo "<td class='Menu'>"; } //if(mysql_num_rows($result_sub_3)>0) else //........................................................................................ if($url_2!="") { echo "<a href='$url_2' target=mainFrame>[".$menu_2[p_id2]."]$name_2 </a>"; }//if($url!="") else { echo "<a>[".$menu_2[p_id2]."]$name_2</a>"; }//if($url!="") else echo "</td></tr> "; if(mysql_num_rows($result_sub_3)>0) { echo "<tr id=Menu".$GLOBALS["ID"]++." style='display:none'>"; echo "<td width='20'> </td>"; echo "<td>"; } $numr_3=mysql_num_rows($result_sub_3); if($numr_3>0) { echo "<table cellpadding='0' cellspacing='0' border='0'>"; //================第三级表头代码 for($rows_3=0;$rows_3<$numr_3;$rows_3++) { $menu_3=mysql_fetch_array($result_sub_3);//将第三级菜单目录导入数组 $url_3=$menu_3['url']; $name_3=$menu_3['topic']; echo "<tr>"; //======================= //提取第四层子菜单记录集 $sql_4="SELECT * FROM tree2 WHERE p_id1 =$menu_3[p_id1] AND p_id2 = $menu_3[p_id2] AND p_id3 = $menu_3[p_id3] AND p_id4 != 0"; $result_sub_4=mysql_query($sql_4); if(mysql_num_rows($result_sub_3)>0) { echo "<td width='20'><img src='foldericon.png' border='0'></td>"; echo "<td class='Menu' onClick='javascript:ShowMenu(Menu".$GLOBALS["ID"].");'>"; } else { echo "<td width='20'><img src='file.png' border='0'></td>"; echo "<td class='Menu'>"; } if($url_3!="") { echo "<a href='$url_3' target=mainFrame>[".$menu_3[p_id3]."]$name_3</a>"; } else { echo "<a>[".$menu_3[p_id3]."]$name_3</a>"; } echo "</td></tr> "; if(mysql_num_rows($result_sub_3)>0) { echo "<tr id=Menu".$GLOBALS["ID"]++." style='display:none'>"; echo "<td width='20'> </td>"; echo "<td>"; } $numr_4=mysql_num_rows($result_sub_4); if($numr_4>0) { echo "<table cellpadding='0' cellspacing='0' border='0'>"; for($rows_4=0;$rows_4<$numr_4;$rows_4++) { $menu_4=mysql_fetch_array($result_sub_4); $url_4=$menu_4['url']; $name_4=$menu_4['topic']; echo "<tr>"; echo "<td width='20'><img src='file.png' border='0'></td>"; echo "<td class='Menu'>"; if($url_4!="") { echo"<a href='$url_4' target=mainFrame>[".$menu_4[p_id4]."]$name_4</a>"; } else { echo "<a>[".$menu_4[p_id4]."]$name_4</a>"; } echo "</td></tr> "; echo "<tr id=Menu".$GLOBALS["ID"]++." style='display:none'>"; echo "<td width='20'> </td>"; echo "<td>"; } } echo "</table>"; } } echo "</table>"; } } } echo "</table >"; } ?> </body> </html> 在这个时候,我的树状菜单是可以用的,但如果我向数据库里添加了东西,就不行了,例如我加上 INSERT INTO `tree2` (`id`, `p_id1`, `p_id2`, `p_id3`, `p_id4`, `topic`, `url`) VALUES (27, 1, 1, 3, 0, 'C部件', ''), (28, 1, 1, 3, 1, 'sbw-c-001', ''); 这个时候我的树状菜单就会不行了, 请教一下各件高手,帮帮我看一看,这个是不是有错,或者说我的思路本来就有错了。 |