时间:2021-07-01 10:21:17 帮助过:20人阅读
public function add($post){
$province = $post['province'];
$citys = $post['city'];
$city_level = $post['city_level'];
var_dump($citys);exit;
$this->province = $province;
$this->city_level = $city_level;
foreach ($citys as $city){
$this->city = $city;
if($this->save()){
return 1;
}else{
return 0;
}
}
}
我想要添加该省份下的城市和改城市对应的级别(A,B,C,D,E),省份和级别都是字符串,但城市可能是一个或多个一个的话就是字符串,多个就是数组,如果为数组改怎么添加数据库(如果有两个城市就要添加两条记录,如 江苏省,苏州市,B ; 江苏省,南京市,B),并且所有数据插入成功要返回1,有一条失败就返回0,求解决
public function add($post){
$province = $post['province'];
$citys = $post['city'];
$city_level = $post['city_level'];
var_dump($citys);exit;
$this->province = $province;
$this->city_level = $city_level;
foreach ($citys as $city){
$this->city = $city;
if($this->save()){
return 1;
}else{
return 0;
}
}
}
数据库其实设计的不好:
id | parent_id | name |
---|---|---|
1 | 0 | 北京 |
2 | 1 | 海淀 |
3 | 1 | 朝阳 |
用一个 parent_id
来控制层级关系即可