// 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();
}
}

其它属性(不常用):

1660

被折叠的 条评论
为什么被折叠?



