MYSQL数据库是这样的:
area_id parent_id area_name
110000 0 北京
110101 110000 东城区
110102 110000 西城区
110103 110000 崇文区
230000 0 黑龙江省
230100 230000 哈尔滨市
230102 230000 道里区
230103 230000 南岗区
310000 0 上海市
310101 310000 黄浦区
310103 310000 卢湾区
...... ...... .....求一个2级联动下拉菜单!
area_id parent_id area_name
110000 0 北京
110101 110000 东城区
110102 110000 西城区
110103 110000 崇文区
230000 0 黑龙江省
230100 230000 哈尔滨市
230102 230000 道里区
230103 230000 南岗区
310000 0 上海市
310101 310000 黄浦区
310103 310000 卢湾区
...... ...... .....求一个2级联动下拉菜单!
<script language = "JavaScript">
var onecount;
onecount=0;
subcat = new Array();
<?
mysql_connect("localhost","root","");
mysql_select_db( "tt" );
$sql="select * from area_district where parent_id !='0'";
$result = mysql_query( $sql );
$count = 0;
while($res = mysql_fetch_row($result))
{
?>
subcat[<?echo$count;?>] = new Array("<?echo$res[2];?>","<?echo$res[1];?>");
<?
$count++;
}
echo "onecount=$count";
?>
function changelocation(locationid)
{
document.myform.ctype.length = 0;
var locationid=locationid;
var i;
for(i=0;i<onecount;i++)
{
if(subcat[i][1] == locationid)
{
document.myform.ctype.options[document.myform.ctype.length] = new Option(subcat[i][0], subcat[i][2]);
}
}
}
</script>
<select name="type" onChange="changelocation(document.myform.type.options[document.myform.type.selectedIndex].value)">
<option selected value="">父</option>
<?
$conn=mysql_connect("localhost","root","");
$sql1="select * from area_district where parent_id='0';";
$rueslt= mysql_db_query('tt',$sql1);
while($res1=mysql_fetch_row($rueslt))
{
?>
<option value="<?echo$res1[0];?>"><?echo$res1[2];?></option>
<?
}
?>
</select>
<select name="ctype">
<option selected value="">子</option>
</select>
</form>
<?php
/***
* function:阶地式调用无限分级列表
* author:manx00
* time:2009-9-11
**/
mysql_connect("localhost","root","");
mysql_select_db("jiedi");
/***********************************
mysql数据库
--------------------
CREATE TABLE `ta` (
`id` int(11) NOT NULL DEFAULT '0',
`pid` int(11) DEFAULT NULL,
`loop` int(11) DEFAULT NULL,
`name` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `ta` VALUES (1, 0, 1, 'a');
INSERT INTO `ta` VALUES (2, 0, 1, 'b');
INSERT INTO `ta` VALUES (3, 0, 1, 'c');
INSERT INTO `ta` VALUES (4, 1, 2, 'aa');
INSERT INTO `ta` VALUES (5, 2, 2, 'bb');
INSERT INTO `ta` VALUES (6, 3, 2, 'cc');
INSERT INTO `ta` VALUES (7, 4, 3, 'aaa');
INSERT INTO `ta` VALUES (8, 4, 3, 'aaa1');
INSERT INTO `ta` VALUES (9, 7, 4, 'aaaa');
************************************/ /***
* 查找主栏目
* $str 为全局变量,这样可以使主栏目与子栏目相关联
*/
function ArrayOne(){
global $str; //最后返回的栏目字符串
$sql = "select * from ta where pid=0;"; //查找主栏目
$result = mysql_query($sql);
if(!$result){
echo "你的sql语句不正确!";
}
if(mysql_num_rows($result) == 0){
//$str = "没有数据!";
return false; //如果没有下级栏目,结束本函数
}
while($row = mysql_fetch_array($result)){
$str .= str_repeat(">",$row['loop']).$row['name']."<br />";
//$str .= str_repeat(">",$row['loop'])."pid:[".$row['pid']."]#id:[".$row['id']."]#name[".$row['name']."]<br />"; //阶梯式输出该主栏目
ArrayChild($row['id']);
}
return $str;
}
/***
* 查找子栏目
* $str与ArrayOne一致
* $pid 为 上级栏目的id
*/
function ArrayChild($pid){
global $str;
$sql = "select * from ta where pid='".$pid."';"; //查找子栏目
$result = mysql_query($sql);
if(!$result){
echo "你的sql语句不正确!";
}
if(mysql_num_rows($result) == 0){
//$str = "没有数据!";
return false;
}
while($row = mysql_fetch_array($result)){
$str .= str_repeat(">",$row['loop']).$row['name']."<br />"; //阶梯式输出该主栏目
ArrayChild($row['id']); //循环调用查找子栏目函数
}
}
echo ArrayOne(); //阶梯式输出全部栏目
?>
ECSHOP有一个自己的省份城市所在表,很好就行JOIN查询,可以去里面取出来。
楼主这样存数据很浪费啊。
mysql> desc ecs_region;
+-------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------------+------+-----+---------+----------------+
| region_id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| parent_id | smallint(5) unsigned | NO | MUL | 0 | |
| region_name | varchar(120) | NO | | | |
| region_type | tinyint(1) | NO | MUL | 2 | |
| agency_id | smallint(5) unsigned | NO | MUL | 0 | |
+-------------+----------------------+------+-----+---------+----------------+
5 rows in set (0.05 sec)
mysql> select * from ecs_region limit 0 ,30;
+-----------+-----------+-------------+-------------+-----------+
| region_id | parent_id | region_name | region_type | agency_id |
+-----------+-----------+-------------+-------------+-----------+
| 1 | 0 | 中国 | 0 | 0 |
| 2 | 1 | 北京 | 1 | 0 |
| 3 | 1 | 安徽 | 1 | 0 |
| 4 | 1 | 福建 | 1 | 0 |
| 5 | 1 | 甘肃 | 1 | 0 |
| 6 | 1 | 广东 | 1 | 0 |
| 7 | 1 | 广西 | 1 | 0 |
| 8 | 1 | 贵州 | 1 | 0 |
| 9 | 1 | 海南 | 1 | 0 |
| 10 | 1 | 河北 | 1 | 0 |
| 11 | 1 | 河南 | 1 | 0 |
| 12 | 1 | 黑龙江 | 1 | 0 |
| 13 | 1 | 湖北 | 1 | 0 |
| 14 | 1 | 湖南 | 1 | 0 |
| 15 | 1 | 吉林 | 1 | 0 |
| 16 | 1 | 江苏 | 1 | 0 |
| 17 | 1 | 江西 | 1 | 0 |
| 18 | 1 | 辽宁 | 1 | 0 |
| 19 | 1 | 内蒙古 | 1 | 0 |
| 20 | 1 | 宁夏 | 1 | 0 |
| 21 | 1 | 青海 | 1 | 0 |
| 22 | 1 | 山东 | 1 | 0 |
| 23 | 1 | 山西 | 1 | 0 |
| 24 | 1 | 陕西 | 1 | 0 |
| 25 | 1 | 上海 | 1 | 0 |
| 26 | 1 | 四川 | 1 | 0 |
| 27 | 1 | 天津 | 1 | 0 |
| 28 | 1 | 西藏 | 1 | 0 |
| 29 | 1 | 新疆 | 1 | 0 |
| 30 | 1 | 云南 | 1 | 0 |
+-----------+-----------+-------------+-------------+-----------+
30 rows in set (0.00 sec)
mysql> SELECT * FROM ecs_region ORDER BY region_id DESC LIMIT 0 ,30;
+-----------+-----------+-------------+-------------+-----------+
| region_id | parent_id | region_name | region_type | agency_id |
+-----------+-----------+-------------+-------------+-----------+
| 3408 | 3401 | 肥西县 | 3 | 0 |
| 3407 | 3401 | 肥东县 | 3 | 0 |
| 3406 | 3401 | 长丰县 | 3 | 0 |
| 3405 | 3401 | 包河区 | 3 | 0 |
| 3404 | 3401 | 蜀山区 | 3 | 0 |
| 3403 | 3401 | 瑶海区 | 3 | 0 |
| 3402 | 3401 | 庐阳区 | 3 | 0 |
| 3401 | 3 | 合肥 | 2 | 0 |
| 3400 | 397 | 澎湖县 | 3 | 0 |
| 3399 | 397 | 花莲县 | 3 | 0 |
| 3398 | 397 | 台东县 | 3 | 0 |
| 3397 | 397 | 屏东县 | 3 | 0 |
| 3396 | 397 | 云林县 | 3 | 0 |
| 3395 | 397 | 南投县 | 3 | 0 |
| 3394 | 397 | 彰化县 | 3 | 0 |
| 3393 | 397 | 苗栗县 | 3 | 0 |
| 3392 | 397 | 桃园县 | 3 | 0 |
| 3391 | 397 | 宜兰县 | 3 | 0 |
| 3390 | 397 | 嘉义 | 3 | 0 |
| 3389 | 397 | 新竹 | 3 | 0 |
| 3388 | 397 | 台南 | 3 | 0 |
| 3387 | 397 | 台中 | 3 | 0 |
| 3386 | 397 | 基隆 | 3 | 0 |
| 3385 | 397 | 高雄 | 3 | 0 |
| 3384 | 397 | 台北 | 3 | 0 |
| 3383 | 396 | 澳门 | 3 | 0 |
| 3382 | 395 | 离岛区 | 3 | 0 |
| 3381 | 395 | 中西区 | 3 | 0 |
| 3380 | 395 | 荃湾区 | 3 | 0 |
| 3379 | 395 | 南区 | 3 | 0 |
+-----------+-----------+-------------+-------------+-----------+
30 rows in set (0.03 sec)
当用户选择了省份的时候,onchange="getCityList()".
这样好节约资源。真的。
第二种方法就是将省份城市全部取出来,使用js Array直接放在网页里面,也是用省份onchange="getCityList()".来。