【实例简介】
【实例截图】
【核心代码】
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
//展示页面
public function index(){
$house=M("json");
$res= $house->select();
$url="http://api.map.baidu.com/telematics/v3/weather?location=%E6%B7%B1%E5%9C%B3&output=json&ak=nfczRD5nihKuHViUZ8ggkUGC";
//获得网站的JSON数据
$data=file_get_contents($url);
//把josn数据转化为数组
$json=json_decode($data,true);
//获得一个二维数组
$arr=$json['results']['0']['index'];
//dump($arr=$json['results']['0']['index']);
//dump($json['results']);
//转化为一位数组
foreach ($arr as $key => $v) {
//只取2个字段加入数据库, 有很多字段不用全部加
$data=array(
"title"=> $v['title'],
"tipt"=> $v['tipt'],
);
}
$house->add($data);
$this->assign("res",$res);
$this->display();
}
//增加界面
public function add(){
$this->display();
}
public function addpro(){
$house=M("json");
$data=array(
'tipt' =>I("post.tipt"),
'title' =>I("post.title"),
);
if($house->data($data)->add()){
$this->success('增加成功', 'index');
}else{
$this->error("增加失败");
}
}
//修改
public function edit(){
$house=M("json");
$id=I("get.id");
$data=array(
"id"=>$id
);
$res= $house->where($data)->find();
$this->assign("res",$res);
$this->display();
}
public function editpro(){
$house=M("json");
$data=array(
"id"=>I("post.id"),
'tipt' =>I("post.tipt"),
'title' =>I("post.title"),
);
if($house->data($data)->save()){
$this->success('修改成功', 'index');
}else{
$this->error("修改失败");
}
}
//删除
public function del(){
$id=I("get.id");
$house=M("json");
$data=array(
"id"=>$id
);
if($house->where($data)->delete()){
$this->success('删除成功', 'index');
}else{
$this->error("删除失败");
}
}
}