zoukankan      html  css  js  c++  java
  • PHP TP模型

    造对象

    1.原始方式(必须做模型文件,即使是空的也要建)

    $model=new HomeModelInfoModel();
    var_dump ($model);

    2.D()方法(不需要建模型文件)

    //$名=D("表名");
    $model=D("Info");
    var_dump ($model);

    3.M()方法(不需要建模型文件)

    //$名=M("表名");
    $nation=M("Nation");
    var_dump($nation);

    操作数据库

    1.调用select方法查询所有数据

    $ainfo=$model->select();

    2.调用select方根据主键值找多条数据

    $ainfo=$model->select("p001,p002,p003");

    3.调用find方法根据主键值找一条特定的数据

    $ainfo=$model->find("p001");

    连贯操作

     where  给查询添加条件

    $ainfo=$model->where("code='p003' or sex=true")->select();

    table  切换操作的表

    $ainfo=$model->table("nation")->select();

    alaias  设置表的别名

    $ainfo=$model->alaias("人员")->select();

    field  指定查询的列

    $ainfo=$model->field("code,name")->select();

    order  对查询结果排序

    $ainfo=$model->order("nation desc")->select();

    group 分组

    $ainfo=$model->field("nation")->group("nation")->select();

    having 加分组后的条件

    $ainfo=$model->group("nation")->having("count(*)>2")->select();

    join  连接多个表(注意:给重名的列加别名)

    $ainfo=$model->field("info.code,info.name as xingming,sex,nation.name,birthday")->join("nation on info.nation = nation.code")->select();

     union  联合查询

    $ainfo=$model->field("name")->union("select name from nation")->select();

    distinct  去重(需要加参数true)

    $ainfo=$model->field("nation")->distinct(true)->select();

    limit  分页(跳过多少条,取多少条)

    $ainfo=$model->limit(2,2)->select();

    page  分页(当前第几页,每页多少条)

    $ainfo=$model->page(3,2)->select();

    聚合函数

    count  取数据总条数

    $ainfo=$model->count("*");

    sum  求和

    $ainfo=$model->table("car")->sum("price");

    avg 求平均数

    $ainfo=$model->table("car")->avg("price");

    max 求最大值

    $ainfo=$model->table("car")->max("price");

    min 求最小值

    $ainfo=$model->table("car")->min("price");

    原生态查询语句

    $sinfo="select * from  info where nation='n002'";
    $ainfo=$model->query($sinfo);

    原生态添加语句

    $sinfo="insert into nation values('n005','傣族')";
    $ainfo=$model->execute($sinfo);

    <if><else /></if>  <foreach></foreach>

    function info()
    {
        $model = D("Info");
        $ainfo = $model->field("Info.Code as code,Info.Name as name,sex,Nation.Name as nationname,birthday")->join("Nation on Info.Nation = Nation.Code")->select();
            
        $this->assign("info",$ainfo);
            
        $this->assign("test",10);
        $this->display();
    }

    info.html

    <table width="70%" border="1" cellpadding="0" cellspacing="0">
        <tr>
            <td>代号</td>
            <td>姓名</td>
            <td>性别</td>
            <td>民族</td>
            <td>生日</td>
        </tr>
        <foreach name="info" item="v">
            <tr>
                <td><{$v.code}></td>
                <td><{$v.name}></td>
               <!-- tp框架中三元运算符暂时不支持.语法-->
                <td><{$v["sex"]?"男":"女"}></td>
                <td><{$v.nationname}></td>
                <td><{$v.birthday}></td>
            </tr>
        </foreach>   
    </table>
    <!--html界面禁止出现“><”应该用备用词代替gt代表>,lt代表<-->
    <if condition="$test gt 5">
    大于5
    <else />
    其他
    </if>
  • 相关阅读:
    docker 批量删除
    ML
    hdu 1465:不容易系列之一(递推入门题)
    sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)
    sdut 2163:Identifiers(第二届山东省省赛原题,水题)
    hdu 2108:Shape of HDU(计算几何,判断多边形是否是凸多边形,水题)
    hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)
    hdu 1312:Red and Black(DFS搜索,入门题)
    hrbustoj 1429:凸多边形(计算几何,判断点是否在多边形内,二分法)
    poj 1113:Wall(计算几何,求凸包周长)
  • 原文地址:https://www.cnblogs.com/yy01/p/5722738.html
Copyright © 2011-2022 走看看