首先,我们来看一下tp框架里面的查询方法:

查询有很多种,代码如下:

<?php
namespace Admin\Controller;
use Think\Controller;
class MainController extends Controller
{
public function showList()
{
echo "大苹果商城";
} public function test()
{
//数据访问
//造模型对象
$nation = D("Nation"); //查询
$a = $nation->select(); //查所有,返回关联数组 $a = $nation->select("n001,n002,n003"); //通过主键查 $a = $nation->find("n002"); //查一条数据 //连贯操作
$a = $nation->where("name='汉族' or name='回族'")->select(); //加条件 $a = $nation->table("Info")->select(); //切换表 $a = $nation->field("name")->select(); //查询指定字段 $a = $nation->order("code desc")->select(); //排序 $a = $nation->limit(3,3)->select(); //分页 $a = $nation->page(3,3)->select(); //分页 $a = $nation->table("Car")->field("Brand,avg(Price)")->group("Brand")->select(); //分组 $a = $nation->table("Car")->field("Brand,avg(Price)")->group("Brand")->having("avg(Price)>50")->select(); $a = $nation->alias('a')->field("b.Code as 'code',b.Name as 'name',a.name as '民族'")->join("Info b on a.Code=b.Nation")->select(); $a = $nation->table("car")->distinct(true)->field("brand")->select(); $a = $nation->where("code='n003'")->getField("name"); //获取某一列的值 $a = $nation->table("car")->sum(Price); //var_dump($a); $sql = "update nation set name='矮人族' where code='n001'";
$a = $nation->query($sql); //执行查询
$a = $nation->execute($sql); //执行其他操作
//var_dump($a);
}
}

然后,我们来看一下数据添加:

新建一个简单的登录界面html文件:

<html>
<head>
<meta content="text/html"; charset="utf-8" />
</head> <body>
<form action="__SELF__" method="post">    <!--这里的__SELF__方法是调用自身-->
<!--name值一定得是数据库的列名-->
<div>代号:<input type="text" name="code" /></div>
<div>民族:<input type="text" name="name" /></div>
<div><input type="submit" value="添加" /></div>
</form>
</body>
</html>

进行添加:

<?php
namespace Admin\Controller; //新建模块需要改模块名
use Think\Controller;
class MainController extends Controller
{
public function zhuCe()
{
//实现两个逻辑
//1.显示注册页面 2.向数据库添加内容 if(empty($_POST))
{
//显示页面
$this->show();
}
else
{
$n = D("rules");
$n->create(); //自动收集表单.前提是必须有post数据
//$n->Name = "hello"; //遇到布尔型数据取一列单独加判断处理
$z = $n->add(); if($z)
{
$this->success("添加成功","zhuCe");
}
else
{
$this->error("添加失败!");
}
} } public function canShu($id=0) //给默认值,不给参数也不会报用户所看不懂的错误。也比较安全
{
//$id = $_GET["id"];
//echo $id; //echo $id;
}
}

重定向(也就是说从一个方法跳到另一个方法):

$this->redirect('zhuCe', array(), 3, '页面跳转中...');

tp框架修改:

后台代码如下:

//主页面显示
public function zhuyemian()
{
$n = D("rules"); //造父类 $attr = $n->select(); //查询
$this->assign("n",$attr); //把查到的数据添加,取名n
$this->show(); //页面显示
} //前提要有一个html页面显示哦。

接下来主题来了:

//修改
public function xiugai($code="") //防止报错存默认值
{
$n = D("rules");
if(empty($_POST))
{
$rules = $n->find($code);
$this->assign("rules",$rules); //存入模版
$this->show(); //显示界面
}
else
{
$n->create();
$r = $n->save();
if($r)
{
$this->success("修改成功!","zhuyemian");
}
else
{
$this->error("修改失败!");
}
}
}

tp框架删除:

//删除
public function shanchu($code)
{
$n = D("rules");
$y = $n->delete($code);
if($y)
{
$url = U("zhuyemian");
$this->success("删除成功!",$url);
}
else
{
$this->error("删除失败!");
}
}

到这里tp框架的增删改查就结束了,再给打架拓展几个输出方法:

//系统变量的输出:
{$Think.server.script_name} // 输出$_SERVER['SCRIPT_NAME']变量
{$Think.session.user_id} // 输出$_SESSION['user_id']变量
{$Think.get.pageNumber} // 输出$_GET['pageNumber']变量
{$Think.cookie.name} // 输出$_COOKIE['name']变量 //常量输出
{$Think.const.MODULE_NAME}
//或直接使用
{$Think.MODULE_NAME} //配置输出
{$Think.config.db_charset}
{$Think.config.url_model} //语言变量
{$Think.lang.page_error}
{$Think.lang.var_error}
04-25 09:50