用mvc实现主要用到了c和v
<?php namespace Home\Controller; use Think\Controller; class MainController extends Controller { public function Main() { var_dump(get_defined_constants(true)); } /*public function Info(){ return "大苹果"; }*/ public function show() { /*$info=D("Info"); var_dump($info); */ $info=M("info");//创建对象 //$info->table("info"); $attr=$info->select();//连贯操作查询 $this->assign("test","hello"); $this->assign("info",$attr); $this->display(); //var_dump($attr); } public function Add() { if(empty($_POST)) { //打出添加页面 $this->display(); } else { //添加数据 $nation=D("Info");//创建表 //$code=$_POST["Code"];//传值 //$name=$_POST["Name"]; //数组方式 //$attr=array("Code"=>$code,"Name"=>$name); //$nation->add($attr); //AR方式 //$nation->Code=$code; //$nation->Name=$name; //$nation->add(); //自动收集表单,数据库名称必须一致 $nation->create(); $z=$nation->add(); if($z) { $this->success("添加成功!","show"); } else { $this->error("添加失败!","Add"); } } } public function Delete() { $code=$_GET["code"]; $info=D("Info"); $z=$info->delete($code); if($z) { $this->success("删除成功",U("show")); } else { $this->error("删除失败",U("show")); } } //数据修改 public function Update() { $code= $_GET["code"]; $info=D("Info"); if(empty($_POST)) { $attr=$info->find($code); $this->assign("info",$attr); $this->display(); } else { //修改数据 /* $info->Code=$_POST['Code']; $info->Name=$_POST["Name"]; $info->Sex=$_POST["Sex"]; $info->Nation=$_POST["Nation"]; $info->Birthday=$_POST["Birthday"];*/ //$code=$_POST["Code"]; $info->create(); $z=$info->save(); if($z) { $this->success("修改成功",U("show")); } else { $this->error("修改失败"); } } } public function test() { $nation=D("Nation"); $sql="select * from Nation"; $a=$nation->query($sql); var_dump($a); //如果是查询query(),如果是其他execute() } }