#区域查询
$province_str = ''; //打印省
$sql = "SELECT * from area_info where SUBSTRING( `internal_code` , 3 ) = '0000'";
$query = $CI->db->query($sql);
foreach ($query->result() as $row)
{
$province_str .= "<option value='".$row->internal_code."'>".$row->name."</option>";
}
$area_str = '';
$area_str = '<select name="sel1" id="sel1" style="width:100px" onChange="GetResult(this.value,1);">
<option value="" selected>--请选择--</option>'.$province_str.'
</select>
<select name="sel2" id="sel2" style="width:100px" onChange="GetResult(this.value,2);">
<option value="" selected>--请选择--</option>
</select>
<select name="sel3" id="sel3" style="width:100px" onChange="GetResult(this.value,3);">
<option value="" selected>--请选择--</option>
</select>';
<script>
function GetResult(str,classid)
{
/*
*--------------- GetResult(str,classid) -----------------
* GetResult(str)
* 功能:通过XMLHTTP发送请求,返回结果.
* 参数:str,字符串,发送条件;classid,数字,菜单级别
* 实例:GetResult(document.all.userid.value,1);
*--------------- GetResult(str,classid) -----------------
*/
//菜单ID数组,定义菜单级数
var MenuIdArr,MenuClass
MenuIdArr= new Array()
MenuClass=3
MenuIdArr[1]="sel1"
MenuIdArr[2]="sel2"
MenuIdArr[3]="sel3"
if(classid == 1 || classid == 2)
{
if (window.XMLHttpRequest) // Mozilla, Safari, ...
{
http_request = new XMLHttpRequest();
}
else if (window.ActiveXObject) // IE
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
var linkurl="<?php echo base_url();?>system/application/views/internet/ajax_get_area.php?typeid="+classid+"&area_code="+str + '&'+Math.random();
http_request.open("GET",linkurl,false);
http_request.send(null);
//服务器端处理返回的是经过escape编码的字符串.
//在页面显示服务器查询结果
var returntxt=unescape(http_request.responseText)
BuildSel(returntxt,document.getElementById(MenuIdArr[classid+1]))
}
f_query.service_area_code.value = str;
//============更改下下级以下菜单为空==============
var kkk
for(kkk=classid+2;kkk<=MenuClass;kkk++)
{
submenu=document.getElementById(MenuIdArr[kkk])
submenu.length=1
submenu.options[0].selected=true
}
}
function BuildSel(str,sel)
{
/*
*--------------- BuildSel(str,sel) -----------------
* BuildSel(str,sel)
* 功能:通过str构建Select.
* 参数:str,字符串,由服务端返回的.有特定结构"字符串1|,字符串2,字符串3"
* 也可为"字符串1序号|字符串1文本,字符串2序号|字符串2文本,字符串3序号|字符串3文本",如本例
* 参数:sel,要构建的Select
* 实例:BuildSel(unescape(oBao.responseText),document.all.sel2)
*--------------- BuildSel(str,sel) -----------------
*/
//先清空原来的数据.
sel.options.length=0;
var arrstr = new Array();
arrstr = str.split("|");
//开始构建新的Select.
sel.options.add(new Option( "--请选择--",""));
if(str.length>0)
{
for(var i=0;i<arrstr.length;i++)
{
//分割字符串
var subarrstr=new Array
subarrstr=arrstr[i].split("-")
//生成下级菜单
sel.options.add(new Option(subarrstr[1],subarrstr[0]));
}
sel.options[0].selected=true
}
}
</script>
2.
<?php
session_start();
#=== 连接数据库 包含数据库 名称 密码等信息 ===
define('BASEPATH','1');
include "../../config/database.php";
#连接数据库
$db_link = @mysql_connect($db['default']['hostname'].":3306", $db['default']['username'], $db['default']['password']);
if( !$db_link )
{
echo '数据库连接失败';
exit();
}
#选择数据库
$SelectResult = @mysql_select_db($db['default']['database'], $db_link);
if( !$SelectResult )
{
echo '选择数据库失败';
exit();
}
$gXmlString = "" ;
if ( $typeid =='1' )
{
#查找某省下的所有市
$sSql = "SELECT internal_code,name from area_info where
SUBSTRING( `internal_code` , 5 ) = '00' and
SUBSTRING( `internal_code` , 1, 2 ) = '".substr($area_code,0,2)."' and
'".$area_code."' != internal_code ORDER BY name";
}
elseif ( $typeid =='2' )
{
#查找某市下的所有区
$sSql = " SELECT internal_code,name FROM area_info where SUBSTRING( `internal_code` , 5 ) <> '00' and SUBSTRING( `internal_code` , 1, 2 ) = '".substr($area_code,0,2)."' and
'".$area_code."' != internal_code ORDER BY name";
}
$query = mysql_query($sSql);
while( $row = mysql_fetch_array($query ))
{
$area_code = $row["internal_code"];
$area_name = $row["name"];
$gXmlString .= $area_code."-".$area_name."|";
}
//$gXmlString = ""."-"."请选择|" . $gXmlString ;
echo substr( $gXmlString, 0, ( strlen($gXmlString) -1 ) ) ;
?>