<p>
排序方式:
<?php $obdy = I(‘get.odby‘, ‘id_desc‘); // 如果odby为空,那么默认是id_desc ?>
<input onclick="this.parentNode.parentNode.submit();" type="radio" name="odby" value="id_desc" <?php if($obdy == ‘id_desc‘) echo ‘checked="checked"‘; ?> /> 以添加时间降序
<input onclick="this.parentNode.parentNode.submit();" type="radio" name="odby" value="id_asc" <?php if($obdy == ‘id_asc‘) echo ‘checked="checked"‘; ?> /> 以添加时间升序
<input onclick="this.parentNode.parentNode.submit();" type="radio" name="odby" value="price_desc" <?php if($obdy == ‘price_desc‘) echo ‘checked="checked"‘; ?> /> 以价格降序
<input onclick="this.parentNode.parentNode.submit();" type="radio" name="odby" value="price_asc" <?php if($obdy == ‘price_asc‘) echo ‘checked="checked"‘; ?> /> 以价格升序
</p>
/***************** 排序 *****************/
$orderby = ‘id‘; // 默认的排序字段
$orderway = ‘desc‘; // 默认的排序方式
$odby = I(‘get.odby‘);
if($odby)
{
if($odby == ‘id_asc‘)
$orderway = ‘asc‘;
elseif ($odby == ‘price_desc‘)
$orderby = ‘shop_price‘;
elseif ($odby == ‘price_asc‘)
{
$orderby = ‘shop_price‘;
$orderway = ‘asc‘;
}
}
/************** 取某一页的数据 ***************/
$data = $this->order("$orderby $orderway") // 排序
->where($where) // 搜索
->limit($pageObj->firstRow.‘,‘.$pageObj->listRows) // 翻页
->select();
-------------------------------------------------------------------
-----------------全部代码----------------------------------------
//实现翻页,搜索和排序
public function search($perPage=15)
{
/*************** 搜索 ******************/
$where = array(); // 空的where条件
// 商品名称
$gn = I(‘get.gn‘);
if($gn)
$where[‘goods_name‘] = array(‘like‘, "%$gn%"); // WHERE goods_name LIKE ‘%$gn%‘
// 价格
$fp = I(‘get.fp‘);
$tp = I(‘get.tp‘);
if($fp && $tp)
$where[‘shop_price‘] = array(‘between‘, array($fp, $tp)); // WHERE shop_price BETWEEN $fp AND $tp
elseif ($fp)
$where[‘shop_price‘] = array(‘egt‘, $fp); // WHERE shop_price >= $fp
elseif ($tp)
$where[‘shop_price‘] = array(‘elt‘, $tp); // WHERE shop_price <= $fp
// 是否上架
$ios = I(‘get.ios‘);
if($ios)
$where[‘is_on_sale‘] = array(‘eq‘, $ios); // WHERE is_on_sale = $ios
// 添加时间
$fa = I(‘get.fa‘);
$ta = I(‘get.ta‘);
if($fa && $ta)
$where[‘addtime‘] = array(‘between‘, array($fa, $ta)); // WHERE shop_price BETWEEN $fp AND $tp
elseif ($fa)
$where[‘addtime‘] = array(‘egt‘, $fa); // WHERE shop_price >= $fp
elseif ($ta)
$where[‘addtime‘] = array(‘elt‘, $ta); // WHERE shop_price <= $fp
/********翻页*******/
//取出总的记录数
$count=$this->where($where)->count();
//生成翻页类的对象
$pageObj=new \Think\Page($count,$perPage);
//设置样式
$pageObj->setConfig(‘next‘,‘下一页‘);
$pageObj->setConfig(‘prev‘,‘上一页‘);
//生成页面下面显示的上一页和下一页
$pageString=$pageObj->show();
/***************** 排序 *****************/
$orderby = ‘id‘; // 默认的排序字段
$orderway = ‘desc‘; // 默认的排序方式
$odby = I(‘get.odby‘);
if($odby)
{
if($odby == ‘id_asc‘)
$orderway = ‘asc‘;
elseif ($odby == ‘price_desc‘)
$orderby = ‘shop_price‘;
elseif ($odby == ‘price_asc‘)
{
$orderby = ‘shop_price‘;
$orderway = ‘asc‘;
}
}
/************** 取某一页的数据 ***************/
$data = $this->order("$orderby $orderway") // 排序
->where($where) // 搜索
->limit($pageObj->firstRow.‘,‘.$pageObj->listRows) // 翻页
->select();
/*********返回数据******/
return array(
‘data‘=>$data, //数据
‘page‘ =>$pageString, //翻页数据
);
}
---------------------------------------------------
<!-- 搜索表单 -->
<div class="form-div">
<form action="__ACTION__" method="GET" name="searchForm">
<P>
商品名称:
<input value="<?php echo I(‘get.gn‘); ?>" type="text" name="gn" size="60" />
</P>
<P>
价 格:
从<input value="<?php echo I(‘get.fp‘); ?>" type="text" name="fp" size="8" />
到<input value="<?php echo I(‘get.tp‘); ?>" type="text" name="tp" size="8" />
</P>
<P>
是否上架:
<?php $ios = I(‘get.ios‘); ?>
<input type="radio" name="ios" value="" <?php if($ios == ‘‘) echo ‘checked="checked"‘; ?> /> 全部
<input type="radio" name="ios" value="是" <?php if($ios == ‘是‘) echo ‘checked="checked"‘; ?> /> 上架
<input type="radio" name="ios" value="否" <?php if($ios == ‘否‘) echo ‘checked="checked"‘; ?> /> 下架
</P>
<P>
添加时间:
从<input type="text" id="fa" name="fa" value="<?php echo I(‘get.fa‘); ?>" size="20" />
到<input type="text" id="ta" name="ta" value="<?php echo I(‘get.ta‘); ?>" size="20" />
</P>
<p>
排序方式:
<?php $obdy = I(‘get.odby‘, ‘id_desc‘); // 如果odby为空,那么默认是id_desc ?>
<input onclick="this.parentNode.parentNode.submit();" type="radio" name="odby" value="id_desc" <?php if($obdy == ‘id_desc‘) echo ‘checked="checked"‘; ?> /> 以添加时间降序
<input onclick="this.parentNode.parentNode.submit();" type="radio" name="odby" value="id_asc" <?php if($obdy == ‘id_asc‘) echo ‘checked="checked"‘; ?> /> 以添加时间升序
<input onclick="this.parentNode.parentNode.submit();" type="radio" name="odby" value="price_desc" <?php if($obdy == ‘price_desc‘) echo ‘checked="checked"‘; ?> /> 以价格降序
<input onclick="this.parentNode.parentNode.submit();" type="radio" name="odby" value="price_asc" <?php if($obdy == ‘price_asc‘) echo ‘checked="checked"‘; ?> /> 以价格升序
</p>
<P>
<input type="submit" value="搜索" />
</P>
</form>
</div>