源码是从csdn转来的,忘了是谁发的了~在此感谢。
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
">
<?php
$host = "localhost";
$db_name = "test";
$username = "root";
$password = "";
$c($host,$username,$password) or die("不能链接到数据库!");
$db=mysql_select_db($db_name,$conn);
//处是连接数据库的代码
$query1=mysql_query("select * from news_bclass where id_2 = '0' order by id asc");
$fMenu="";
$fValue="";
while($data1=mysql_fetch_array($query1)){
$fMenu.="\"".$data1[bname]."\",";
$fValue.="\"".$data1[id]."\",";
}
$fMenu=substr($fMenu,0,(strlen($fMenu)-1));
$fMenu="[".$fMenu."]";//*****************************得到var fMenu
$fValue=substr($fValue,0,(strlen($fValue)-1));
$fValue="[".$fValue."]";//*****************************得到var fValue
//得到*****************************var sMenu
$query1=mysql_query("select * from news_bclass where id_2 = '0' order by id asc");
while($data1=mysql_fetch_array($query1)){
$province_id1=$data1[id];
$query2=mysql_query("select * from news_sclass where parentid='$province_id1' order by parentid desc");
while($data2=mysql_fetch_array($query2)){
$num=mysql_num_rows($query2);
$i++;
$sMenu.="\"".$data2[sname]."\",";
if($i==$num){
$sMenu="[".$sMenu."],[";
$i=0;
}
}
}
$sMenu.="]";
$sMenu=str_replace("\",]","\"]",$sMenu);
$sMenu=str_replace(",[]","]",$sMenu);
if((substr($sMenu,0,2))=="[\""){
$sMenu="[".$sMenu;
}else{
$sMenu=preg_replace("/\[{3,}/is","[[\\1",$sMenu);//匹配
}
//得到*****************************var sValue
$query1=mysql_query("select * from news_bclass where id_2 = '0' order by id asc");
while($data1=mysql_fetch_array($query1)){
$province_id=$data1[id];
$sql="select * from news_sclass where parentid='$province_id' order by parentid asc";
// echo $sql;
$query2=mysql_query($sql);
while($data2=mysql_fetch_array($query2)){
$nums=mysql_num_rows($query2);
$j++;
$sValue.="\"".$data2[id]."\",";
if($j==$nums){
$sValue="[".$sValue."],[";
$j=0;
}
}
}
$sValue.="]";
$sValue=str_replace("\",]","\"]",$sValue);
$sValue=str_replace(",[]","]",$sValue);
if((substr($sValue,0,2))=="[\""){
$sValue="[".$sValue;
}else{
$sValue=preg_replace("/\[{3,}/is","[[\\1",$sValue);//匹配
}
?>
数据库表结构:
-- phpMyAdmin SQL Dump
-- version 2.9.2
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2007 年 10 月 14 日 03:31
-- 服务器版本: 5.0.27
-- PHP 版本: 5.2.1
--
-- 数据库: `test`
--
-- --------------------------------------------------------
--
-- 表的结构 `news_bclass`
--
CREATE TABLE `news_bclass` (
`id` int(11) NOT NULL auto_increment,
`bname` varchar(10) NOT NULL default '',
`id_2` varchar(12) NOT NULL,
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
--
-- 导出表中的数据 `news_bclass`
--
INSERT INTO `news_bclass` VALUES (1, '123', '0');
INSERT INTO `news_bclass` VALUES (2, '3123', '1');
INSERT INTO `news_bclass` VALUES (3, '321231', '1');
INSERT INTO `news_bclass` VALUES (4, '4444', '0');
INSERT INTO `news_bclass` VALUES (5, '23123', '0');
-- --------------------------------------------------------
--
-- 表的结构 `news_sclass`
--
CREATE TABLE `news_sclass` (
`id` int(11) NOT NULL auto_increment,
`sname` varchar(10) NOT NULL default '',
`parentid` int(10) NOT NULL default '0',
`bname` varchar(10) NOT NULL default '',
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
--
-- 导出表中的数据 `news_sclass`
--
INSERT INTO `news_sclass` VALUES (1, '123', 0, '123');
INSERT INTO `news_sclass` VALUES (2, '12312', 1, '123');
INSERT INTO `news_sclass` VALUES (3, '32123', 1, '231');
INSERT INTO `news_sclass` VALUES (4, '12312', 1, '123');
INSERT INTO `news_sclass` VALUES (5, '32123', 1, '231');
INSERT INTO `news_sclass` VALUES (6, '4444', 4, '4444');
INSERT INTO `news_sclass` VALUES (7, '4444', 4, '4444');