zoukankan      html  css  js  c++  java
  • 数据库访问CRUD;__SELF__和__ACTION__的区别;自动收集表单:$n->create();

    一、tp框架数据访问(pdo基础)

    public function test()
    {
      $n = D("Nation");
    
     //select();find();    //查询
    
      1.$attr = $n->select();//查所有,返回一个关联数组,二维关联数组
      2.$attr = $n->select("n001,n002");//根据参数(主键值)查
      3.$attr = $n->find("n002");//查一条数据,一维数组,参数必须是主键值
     //where    //连贯操作
      4.$n->where()->select();//加条件,$n->where()返回一个对象 
        $n->where("name=>'汉族' or name='回族'")->select();
      5.table    //如果查Nation表以外的数据,用table
        $attr = $n->table("info")->select();
      6.field    //指定字段查询(查某几列)
        $attr = $n->field("code")->select();
      7.order    //排序
        $attr = $n->order("code desc")->select();
      8.limit    //分页
        $a = $n->limit(3,5)->select();//跳过3条数据取5条数据
        $a = $n->limit(3)->select();//取3条数据
      9.page    //分页
        $a = $n->page(3,5)->select();//第3页显示5条数据,根据条数来自动分页
      10.group    //分组
        $a = $nation->field("Brand,avg(Price)")->group("Brand")->select();
      11.having    //分完组加条件
        $a = $nation->field("Brand,avg(Price)")->group("Brand")->having("avg(Price)>40")->select();
      12.distinct    //去重
        $a = $nation->field("Brand")->distinct(true)->select();
      13.getField    //获取某字段的数据,只能给列名查询,不写where条件,默认给索引最小的字段
        $a = $nation->where("code='n001'")->getField("name");
      14.sum,count,max,min 
        $a = $nation->table("car")->sum(Price);
      15.join..on    //给索要查询的列都 as 给一个别名显示,别名自定义即可    
        $a = $nation->field("Info.code as 'code',Info.name as 'name',Nation.name as '民族'")->join("Info on Info.nation=Nation.code")->select();
      16.cache    //数据缓存
      17.query    //原生sql语句查询
        $sql = "select * from nation";
        $a = $nation->query($sql);
      18.execute    //原生sql语句其他操作 修改    
        $sql = "update Nation set name='满族' where code='n001'";
        $a = $nation->execute($sql);
    
      var_dump($a);
    }
    View Code

    二、数据添加

    <?php
    namespace ZuoyeController;
    use ThinkController;
    class MainController extends Controller
    {
        public function test()
        {
            //造模型对象
            $n = D("Nation");
            
            //使用数组的方式添加
            $attr = array("Code"=>"n100","Name"=>"汉族");
            $n->add($attr);
            
            //使用AR的方式
            $n->Code="n099";
            $n->Name="回族";
            $n->add();
            
        }    
        public function _empty()
        {
            echo "连接失败!";
        }
    }
    View Code

    三、通过地址栏传参数,如:Main/canshu/id/5,方法来接收使用

    <?php
    namespace LianxiController;
    use ThinkController;
    class MainController extends Controller
    {
        //通过地址栏传参数,如:Main/canshu/id/5,方法来接收使用
        public function canshu($id)
        {
            //方法一
            //echo $_GET["id"];    
            //方法二:canshu($id)
            echo $id;//如果给$id赋值canshu($id=0)时,地址栏不给参数也不会报错
        }
    }
    View Code

    四、自动收集表单(功能强大,把表单里面的内容用create方法快速收集)

    Application/Lianxi/controller/MainController.class.php

    <?php
    namespace LianxiController;
    use ThinkController;
    class MainController extends Controller
    {
        public function Biaodan()
        {
            //实现两个逻辑
            //1.显示添加页面2.往数据库添加数据
            if(empty($_POST))
            {
                $this->show();    
            }
            else
            {
                $n = D("Nation");
                $n->create();//自动收集表单,前提是必须有post数据,如果post为空,走if;html页面中input里面的name一定要对应数据库表的的列名,大小写敏感    
                $z = $n->add();
                if($z)
                {
                    //跳转方法一
                    //$this->success("添加成功","Biaodan",5);//5是跳转时间,间隔5秒
                    //跳转方法二
                    $this->redirect('Biaodan',array(),5,"页面跳转中...");//array('id'=>2),跳转页面的同时可以传参数,关联    
                }
                else
                {
                    $this->error("添加失败!");//一个参数就可以,error失败默认返回前一个页面    
                }
            }
        }    
    }
    View Code

    Application/Lianxi/view/Main/Biaodan.html

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
    <form action="__SELF__" method="post">
    <div>代号:<input type="text" name="Code" /></div>
    <div>名称:<input type="text" name="Name" /></div>
    <input type="submit" value="添加" />
    </form>
    </body>
    </html>
    View Code

    1.跳转页面失败后如何把错误信息去掉的问题:

    2.在表单<form>里面的action指向哪里的问题:指向__SELF__或者__ACTION__

    先var_dump(get_defined_constants(true));显示出系统常量,找到__SELF__和__ACTION__;

     __SELF和__ACTION__的区别:__SELF__访问的是自身,就是浏览器访问的地址,如:localhost/tp/index.php/Home/Main/Biaodan/code/10(code和10是随意传的参数),__ACTION__只能访问到Biaodan(方法)这里

  • 相关阅读:
    Activeandroid的使用
    Android的数据库ORM框架:Sugar ORM
    Android 快速开发系列 ORMLite 框架最佳实践
    Android ORM 框架之 greenDAO 使用心得
    最受欢迎的5个Android ORM框架
    Android butterknife框架 注解攻略
    强烈推荐android studio用的几个插件
    Android4.0(Phone)来电过程分析
    设计模式入门之装饰器模式Decorator
    VB.NET之错误异常处理
  • 原文地址:https://www.cnblogs.com/zxl89/p/6219406.html
Copyright © 2011-2022 走看看