PHP 省市二级菜单类

PHP 省市二级菜单类——封装省市联动的方法,ProvinceAndCityFactory省市二级菜单类将部分常用的省市二级导航菜单数据封装到类中,将省级数据存储到getProvince()方法中,将市级数据存储到getCityByProvinceArray()方法中。这样在需要应用这一功能的地方直接调用类中的方法即可。

PHP 省市二级菜单类 语法

/*********************************************
 * @ 说明:省市二级菜单类
 *********************************************/
class ProvinceAndCityFactory{
   /**
   * 返回省份
   *
   * @return array()
   */
   public static function getProvince (){
      provinceArray = array('-1' => '请选择省份' , '北京' , '上海' , '天津' , '重庆' ,
'黑龙江' , '吉林' , '辽宁' , '内蒙古' , '河北' , '河南' , '山东' , '山西' , '江苏' , '安
徽' , '陕西' , '宁夏' , '甘肃' , '青海' , '湖北' , '湖南' , '浙江' , '江西' , '福建' ,
'贵州' , '四川' , '广东' , '广西' , '云南' , '海南' , '新疆');
      returnprovinceArray;
   }
   /**
   * 根据省份数组索引返回城市
   *
   * @param int index
   * @return arry()
   */
   public static function getCityByProvinceArray (index){
      city = array();city[0] = array("北京");
      city[1] = array("上海");city[2] = array("天津");
      city[3] = array("重庆");city[4] = array("哈尔滨" , "齐齐哈尔" , "牡丹江" , "大庆" , "伊春" , "双鸭山" ,
"鹤岗" , "鸡西" , "佳木斯" , "七台河" , "黑河" , "绥化" , "大兴安岭");
      city[5] = array("长春" , "延边" , "吉林" , "白山" , "白城" , "四平" , "松原" ,
"辽源" , "大安" , "通化");city[6] = array("沈阳" , "大连" , "葫芦岛" , "旅顺" , "本溪" , "抚顺" , "铁岭" ,
"辽阳" , "营口" , "阜新" , "朝阳" , "锦州" , "丹东" , "鞍山");
      city[7] = array("呼和浩特" , "呼伦贝尔" , "锡林浩特" , "包头" , "赤峰" , "海拉尔" ,
"乌海" , "鄂尔多斯" , "通辽");city[8] = array("石家庄" , "唐山" , "张家口" , "廊坊" , "邢台" , "邯郸" , "沧州" ,
"衡水" , "承德" , "保定" , "秦皇岛");
      city[9] = array("郑州" , "开封" , "洛阳" , "平顶山" , "焦作" , "鹤壁" , "新乡" ,
"安阳" , "濮阳" , "许昌" , "漯河" , "三门峡" , "南阳" , "商丘" , "信阳" , "周口" , "驻马店");city[10] = array("济南" , "青岛" , "淄博" , "威海" , "曲阜" , "临沂" , "烟台" ,
"枣庄" , "聊城" , "济宁" , "菏泽" , "泰安" , "日照" , "东营" , "德州" , "滨州" ,
"莱芜" , "潍坊");
      city[11] = array("太原" , "阳泉" , "晋城" , "晋中" , "临汾" , "运城" , "长治" ,
"朔州" , "忻州" , "大同" , "吕梁");city[12] = array("南京" , "苏州" , "昆山" , "南通" , "太仓" , "吴县" , "徐州" ,
"宜兴" , "镇江" , "淮安" , "常熟" , "盐城" , "泰州" , "无锡" , "连云港" , "扬州" ,
"常州" , "宿迁");
      city[13] = array("合肥" , "巢湖" , "蚌埠" , "安庆" , "六安" , "滁州" , "马鞍山" ,
"阜阳" , "宣城" , "铜陵" , "淮北" , "芜湖" , "毫州" , "宿州" , "淮南" , "池州");city[14] = array("西安" , "韩城" , "安康" , "汉中" , "宝鸡" , "咸阳" , "榆林" ,
"渭南" , "商洛" , "铜川" , "延安");
      city[15] = array("银川" , "固原" , "中卫" , "石嘴山" , "吴忠");city[16] = array("兰州" , "白银" , "庆阳" , "酒泉" , "天水" , "武威" , "张掖" ,
"甘南" , "临夏" , "平凉" , "定西" , "金昌");
      city[17] = array("西宁" , "海北" , "海西" , "黄南" , "果洛" , "玉树" , "海东" , "海南");city[18] = array("武汉" , "宜昌" , "黄冈" , "恩施" , "荆州" , "神农架" , "十堰" ,
"咸宁" , "襄樊" , "孝感" , "随州" , "黄石" , "荆门" , "鄂州");
      city[19] = array("长沙" , "邵阳" , "常德" , "郴州" , "吉首" , "株洲" , "娄底" ,
"湘潭" , "益阳" , "永州" , "岳阳" , "衡阳" , "怀化" , "韶山" , "张家界");city[20] = array("杭州" , "湖州" , "金华" , "宁波" , "丽水" , "绍兴" , "雁荡山" ,
"衢州" , "嘉兴" , "台州" , "舟山" , "温州");
      city[21] = array("南昌" , "萍乡" , "九江" , "上饶" , "抚州" , "吉安" , "鹰潭" ,
"宜春" , "新余" , "景德镇" , "赣州");city[22] = array("福州" , "厦门" , "龙岩" , "南平" , "宁德" , "莆田" , "泉州" ,
"三明" , "漳州");
      city[23] = array("贵阳" , "安顺" , "赤水" , "遵义" , "铜仁" , "六盘水" , "毕节" ,
"凯里" , "都匀");city[24] = array("成都" , "泸州" , "内江" , "凉山" , "阿坝" , "巴中" , "广元" ,
"乐山" , "绵阳" , "德阳" , "攀枝花" , "雅安" , "宜宾" , "自贡" , "甘孜州" , "达州" ,
"资阳" , "广安" , "遂宁" , "眉山" , "南充");
      city[25] = array("广州" , "深圳" , "潮州" , "韶关" , "湛江" , "惠州" , "清远" ,
"东莞" , "江门" , "茂名" , "肇庆" , "汕尾" , "河源" , "揭阳" , "梅州" , "中山" ,
"德庆" , "阳江" , "云浮" , "珠海" , "汕头" , "佛山");city[26] = array("南宁" , "桂林" , "阳朔" , "柳州" , "梧州" , "玉林" , "桂平" ,
"贺州" , "钦州" , "贵港" , "防城港" , "百色" , "北海" , "河池" , "来宾" , "崇左");
      city[27] = array("昆明" , "保山" , "楚雄" , "德宏" , "红河" , "临沧" , "怒江" ,
"曲靖" , "思茅" , "文山" , "玉溪" , "昭通" , "丽江" , "大理");city[28] = array("海口" , "三亚" , "儋州" , "琼山" , "通什" , "文昌");
      city[29] = array("乌鲁木齐" , "阿勒泰" , "阿克苏" , "昌吉" , "哈密" , "和田" ,
"喀什" , "克拉玛依" , "石河子" , "塔城" , "库尔勒" , "吐鲁番" , "伊宁");
      returncity[$index];
   }
}

PHP 省市二级菜单类 示例

在本实例中延续上一实例的功能,在验证码的基础上,添加一个省市二级导航菜单功能。同样通过jQuery在客户端完成对用户登录信息的验证。

由于本实例是上一实例验证码类的延续,所以有关用户登录的基本功能这里不再赘述。调用省市二级菜单类生成省市二级导航菜单的关键步骤如下:
(1)封装ProvinceAndCityFactory省市二级菜单类,并将其存储于ProvinceAndCityFactory.php文件中。
(2)在index.php文件的表单中,添加select标签,设置省市二级导航菜单。其关键代码如下:

<td width="108" align="right">城  市:</td>
<td>
<select id="p" name="p" onchange="changeCity()" >
        <option>-请选择地区-</option>
        <?php
        include("ProvinceAndCityFactory.php");         //包含省市二级菜单类
        ProvinceAndCity=new ProvinceAndCityFactory(); //执行类的实例化city=ProvinceAndCity->getProvince();         //获取省份数据counts = count(city);                        //将省份数据写入到下拉列表中
        for (i = 0; i<counts; i ++) {cityStr .= '<option value="' . i . '">' .city[i] . '</option>';
        }
        echocityStr;                                 //输出下拉列表的值
        ?>
  </select>
   <select id="c" name="c"></select>
</td>

(3)在JavaScript脚本中,定义changeCity()方法,实现在不同城市之间的切换操作。其代码如下:

function changeCity(){                                 //切换城市
    .get("selectcity.php?pindex="+("#p").val(), null, function(data){
       ("#c").empty();("#c").append(data);                           //添加数据
       if(.trim(("#c").val())=="-1"){
           ("#p").css("border", "1px solid #FF0000");("#c").css("border", "1px solid #FF0000");
       }else{
           ("#p").css("border", "1px solid #777777");("#c").css("border", "1px solid #777777");
       }
     });
   }

(4)创建selectcity.php文件,根据省份下拉列表框中的值,为市级下拉列表框中的变量赋值。其关键代码如下:

<?php
header('content-type:text/html; charset=utf-8');       //设置页面编码格式
include("ProvinceAndCityFactory.php");                 //包含省市二级菜单类
ProvinceAndCity=new ProvinceAndCityFactory();         //执行类的实例化操作pindex=_GET['pindex'];                               //获取超级链接传递的值cityStr = '';
if (pindex == '-1') {cityStr .= '<option value="-1">请选择城市</option>';
} else {
     cityArray =ProvinceAndCity->getCityByProvinceArray(pindex);
                                             //读取市级数组中的元素值count = count(cityArray);
     for (i = 0; i<count; i ++) {      //将市级数组中的数据赋给下拉列表框中的变量cityStr .= '<option value="' . i . '">' .cityArray[i] . '</option>';
     }
}
echocityStr;
?>

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程