require 'city.php'
// 连接数据库(PDO)
$pdo=new PDO('mysql:host=localhostdbname=city','root','root')
$pdo->exec('set names utf8')
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC)
// 实例化城市类
$c=new city()
$result=$c->mycity()
$city_id = 0
foreach ($result as $k=>$value) {
if(count($value) == count($value,1)){
$sql="insert into city_province (province) value ('{$value['province_name']}')"
$smt=$pdo->prepare($sql)
$smt->execute()
}else{
$sql11="insert into city_province (province) value ('{$value['province_name']}')"
$smt11=$pdo->prepare($sql11)
$smt11->execute()
foreach ($value['city'] as $v=>$value1) {
$sql2="insert into city_name (name,province_id) values ('{$value1['city_name']}','{$k}')"
$smt2=$pdo->prepare($sql2)
$smt2->execute()
$city_id += 1
echo $city_id.'<br>'
if(!empty($value1['area'])){
foreach ($value1['area'] as $valu1e2) {
$sql3="insert into city_area (area,city_id) values ('{$valu1e2}','{$city_id}')"
$smt3=$pdo->prepare($sql3)
$smt3->execute()
}
}
}
}
}
城市类太大了,发不了。
我是把省、市、地区分别存在三个数据表中(我是新手,刚自己写的)
id fid name跟分类一个思想,
城市表 增加个fid用于记录所属省的ID
省级则fid为0
更具省id取出相应城市 select * form city where fid = id
根据省名称,就先查出省对应的id,再查对应的城市
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)