效果图
分析
index模块下/index控制器/index方法
1、访问index.php/index/index/index,是以get请求查询,数据过多有分页效果,但是页码跳转,不需要携带搜索条件。
2、填写搜索条件,然后点击“查询”按钮,是以post请求查询。刷新页面后,很有必要将搜索条件回显出来。
3、而post请求查询的数据比较多,有分页效果的时候。点击页码跳转,是get请求,但是必须携带上查询条件,毫无疑问是通过url的参数来携带。同时,页码跳转后,也很有必要将搜索条件回显出来。
经过一顿分析,似乎有点乱。不知道从哪入手。
下面结合核心代码看看,如何用比较 简洁的代码和逻辑 实现上述效果。
只需关注代码逻辑即可。
FlowerModel.php
<?php
namespace app\index\model;
use think\Model;
use think\Db;
use think\db\Query;
/**
* @author passerbyYSQ
* @create 2020年4月30日 下午2:22:02
*/
class FlowerModel extends Model {
/**
* @param unknown $condition 数组,查询条件
* @param unknown $condition 每页显示的数据的条数
*/
public function flowerPage($condition, $count = 8) {
$name = $condition['fname']; // 名字
$category = $condition['fclass']; // 种类
$low = $condition['minprice']; // 最低价格
$high= $condition['maxprice']; // 最高价格
/*
* $condition这个数组是Index控制器传递过来的,数组必定
* 有上面4个索引。如果没有对应的查询条件,对应的元素为
* 空字符串
*
* 在这里,我们无需考虑,这些搜索条件,是通过url参数获得
* 还是post表单提交获得。因为这是Index控制器的事情。
* 这个方法只需要考虑,你给我某些条件(4个条件不一定全都有),
* 我做带分页的条件查询,并结果返回给Index控制器。
*/
$query = new Query();
$query = $query->table('flower');
$parms = array();
// 组织Query对象。并将【不为空的条件】,存到$parms数组,用于拼接到url后面
if ($name != '') {
$query = $query->where('fname', 'like', '%' . $name . '%');
$parms['fname'] = $name;
}
if ($category != '') {
$query = $query->where('fclass1', '=', $category);
$parms['fclass'] = $category;
}
if ($low != '') {
$query = $query->where('yourprice', '>=', intval($low));
$parms['minprice'] = $low;
}
if ($high != '') {
$query = $query->where('yourprice', '<=', intval($high));
$parms['maxprice'] = $high;
}
$query = $query->order(['SelledNum'=>'desc', 'price'=>'asc']);
// tp 5的分页逻辑已经封装好了。直接调用paginate()即可
// 第3个参数:拼接在分页的url后面的参数
$paginator = $query->paginate($count, false, ['query' => $parms]);
// 获取分页按钮栏的Html代码
$data['pageHtml'] = $paginator->render();
// 当前页的数据
$data['flowers'] = $paginator->items();
return $data;
}
}
?>
Index.php(控制器)
<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use think\Request;
use app\index\model\FlowerModel;
class Index extends Controller
{
/*
* http://localhost/flowerTP/index/index/index/index
* 4个index
* 1、tp5的入口文件index.php。可省略.php
* 2、模块。application/index
* 3、控制器类。application/index/controller/Index.php
* 4、对应控制器类中的方法。Index控制器中的index方法
*/
public function index(Request $request)
{
// 实例化Model对象
$model = new FlowerModel();
// 获取分类树。显示到Select标签里面。不是核心代码,无需关心
$categories = $model->getAllCategory();
/*
* 经过前面分析可知,搜索添加有可能来自url参数(页码跳转时携带)
* 或者post表单提交。所以,参数的接收,需要用$request->param()
*
* $a ?? ''; 等价于 isset($a) ? $a : '';
*
* 问题1:表单参数有可能不存在吗?
* 在访问index.php/index/index时(进入该页面),由于post表单未提交,
* 而url中又不带有这些参数。所以这些表单参数是不存在。这个时候通过直接
* 通过$request->param()取出必定报错。所以必须要用isset判断。
* 问题2:
* 为什么某个表单参数不存在时,需要给它赋值为空字符串?
* 1、FlowerModel里面方法flowerPage()的需要
* 2、将$conditions丢给页面回显搜索条件时,不需要判断$conditions
* 中的字段是否存在,因为必定存在,只不过有可能是空字符串而已。空字符串
* 直接显示即可
*/
$conditions=array();
$conditions['fname'] = $request->param('fname') ?? '';
$conditions['fclass'] = $request->param('fclass') ?? '';
$conditions['minprice'] = $request->param('minprice') ?? '';
$conditions['maxprice'] = $request->param('maxprice') ?? '';
// 丢给Model去查询数据
$res = $model->flowerPage($conditions);
//var_dump($condition);
// 绑定变量,用于渲染页面
$this->assign('categories', $categories);
$this->assign('flowers', $res['flowers']);
$this->assign('pageHtml', $res['pageHtml']);
$this->assign('conditions', $conditions);
return $this->fetch();
}
?>
index.html
<div id="search_div">
<form method="post" action="{:url('index/index/index')}">
<span class="input_span">商品名:<input type="text" name="fname" value="{$conditions.fname}"/></span>
<span class="input_span">商品种类:
<select name="fclass">
<!--
注意一个很重要的点:
不限这个Option的value一定是空字符串。post表单提交后,到后台对组织Query才不会造成影响
-->
<Option value="">不限</option>
{foreach $categories as $category}
<Option value="{$category}" {if condition="$category eq $conditions.fclass"}selected="selected"{/if}>{$category}</option>
{/foreach}
</select>
</span>
<span class="input_span">商品价格区间:<input type="text" name="minprice" value="{$conditions.minprice}"/> - <input type="text" name="maxprice" value="{$conditions.maxprice}"/></span>
<input type="submit" value="查 询">
</form>
</div>
整个代码逻辑是不是比较清晰简洁?对于实现这种类型的业务,直接copy上面的业务代码。来修改就行了。