tp请求管理

//        6.请求数据 get
//        print_r($_GET);
//        print_r(Request::get());
//        print_r(Request::get('id'));

        //param获取get和post的参数
//        print_r(Request::param());
//        print_r(Request::post());

模仿商城查询的方式

//        7.模仿商城查询的方式
        $where[]=['status','=',1];
        $print_min = Request::post('print_min',0);
        if(!empty($print_min)){
            $where[]=['price','>=',$print_min];
        }

        $print_max = Request::post('print_max',0);
        if(!empty($print_max)){
            $where[]=['price','<=',$print_max];
        }

        $sum = Request::post('sum',0);
        if(!empty($sum)){
            $where[]=['price','>=',$sum];
        }

        $lists = Db::table('app_book1')->where($where)
                                ->order('price DESC')
                                ->select();
        
        print_r($lists);

强制转换:

//        7.模仿商城查询的方式
        $where[]=['status','=',1];
        $print_min = Request::post('print_min/d',0);
        if(!empty($print_min)){
            $where[]=['price','>=',$print_min];
        }

        $print_max = Request::post('print_max',0);
        if(!empty($print_max)){
            $where[]=['price','<=',$print_max];
        }

        $sum = Request::post('sum',0);
        if(!empty($sum)){
            $where[]=['price','>=',$sum];
        }

        $lists = Db::table('app_book1')->where($where)
                                ->order('price DESC')
                                ->select();
        
        print_r($lists);

请求类型:

//        7.模仿商城查询的方式
        if(Request::method() == 'POST){
        $where[]=['status','=',1];
        $print_min = Request::post('print_min/d',0);
        if(!empty($print_min)){
            $where[]=['price','>=',$print_min];
        }

        $print_max = Request::post('print_max',0);
        if(!empty($print_max)){
            $where[]=['price','<=',$print_max];
        }

        $sum = Request::post('sum',0);
        if(!empty($sum)){
            $where[]=['price','>=',$sum];
        }

        $lists = Db::table('app_book1')->where($where)
                                ->order('price DESC')
                                ->select();
}
        
        print_r($lists);

最终的 $where 结构示例
假设用户同时提交了 print_min=10 和 print_max=100,则 $where 数组最终为

[
    ['status', '=', 1],
    ['price', '>=', 10],
    ['price', '<=', 100]
]

访问网址:

echo Request::host();

获取时间

echo Request::time();

修改时间格式

echo date('Y-m-d H:i:s',Request::time());

模型

model

ShopLists.php:

index.php:

shoplists可以把返回值改成数组:

指定表

name,table,pk区别:

class ShopLists extends Model{
//    protected $table = 'app_book1';  //完整表
    protected $name = 'book1'; //name可以省略表前缀
    protected $pk = 'cid';  //指定主键
    public function get_data(){
//        $ret = ShopLists::find(6);
//        $ret = ShopLists::where('id',10)->find();

        $ret = static::select();

        //得返回
        return $ret;
//        return $ret->toArray();

schema作用:

class ShopLists extends Model{
//    protected $table = 'app_book1';  //完整表
    protected $name = 'book1'; //name可以省略表前缀
    protected $pk = 'cid';  //指定主键

    protected $schema = [
        'cid'=>'int',
        'pid'=>'int',
        'name'=>'string',
        'pic'=>'string',
        'sort'=>'int',
        'status'=>'int',
    ];

    public function get_data(){
        $ret = ShopLists::find(6);
//        $ret = ShopLists::where('id',10)->find();
        
//        $ret = static::select();
        
        //得返回
//        return $ret;
        return $ret->toArray();
    }
}

disuse废弃字段: 

class ShopLists extends Model{
//    protected $table = 'app_book1';  //完整表
    protected $name = 'book1'; //name可以省略表前缀
    protected $pk = 'cid';  //指定主键

    protected $schema = [
        'cid'=>'int',
        'pid'=>'int',
        'name'=>'string',
        'pic'=>'string',
        'sort'=>'int',
        'status'=>'int',
    ];

    //废弃字段 当这个字段完完全全用不到的时候,才使用废弃字段
    protected $disuse = [
        'pic',
        'status'
    ];

    public function get_data(){
        $ret = ShopLists::find(6);
//        $ret = ShopLists::where('id',10)->find();

//        $ret = static::select();

        //得返回
//        return $ret;
        return $ret->toArray();
    }
}

其它属性(不常用):

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值