tp5中多条件搜索分页,并excel导出

1:视图层

<div class="page-content">
<!-- <div class="row"> -->
<form action="{:url('Jgymuser/saomiao')}" method="post">


<input type="text" name="username" value="" placeholder="请输入微信昵称" style="float:left;margin-left:10px;width:115px;margin-top:10px;margin-bottom:10px;" />
<input type="text" name="city" value="" placeholder="请输入城市" style="float:left;margin-left:15px;width:100px;margin-top:10px;margin-bottom:10px;" />
<input type="text" name="ph" value="" placeholder="请输入批号" style="float:left;margin-left:18px;width:100px;margin-top:10px;margin-bottom:10px;" />
<input type="text" name="con" value="" placeholder="请输入扫描商品" style="float:left;margin-left:25px;width:110px;margin-top:10px;margin-bottom:10px;" />
<input type="text" name="ip" value="" placeholder="请输入ip" style="float:left;margin-left:30px;width:120px;margin-top:10px;margin-bottom:10px;" />
<div class="col-xs-12 col-sm-3" style="float:right;margin-right:200px;margin-top:-45px;margin-left: 980px;">
<!-- <label for="id-date-picker-1">时间段选择日期表单</label> -->
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-calendar bigger-110"></i>
</span>
<input class="form-control" type="text" name="date-range-picker" id="id-date-range-picker-1" />
</div>

</div>
<input type="submit" class="btn btn-success" value="搜索" style="float:left;margin-left: 930px;margin-top:-45px;border: 1px solid #FFF;">
</form>
<a href="{:url('Jgymuser/Excelout',array('username'=>"$username",'city'=>"$city",'ph'=>"$ph",'con'=>"$con",'ip'=>"$ip"))}" class="btn btn-success" style="float:right;margin-top:-50px;margin-bottom:10px;margin-right:13px;">
Excel导出
</a>
<!-- /section:settings.box -->
<div class="page-content-area">


<div class="row">
<div class="col-xs-12">
<!-- PAGE CONTENT BEGINS -->


<div class="col-xs-12">
<table id="sample-table-1" class="table table-striped table-bordered table-hover">

<thead>
<tr>
<th>扫描次数</th>
<th>微信昵称</th>
<th>性别</th>
<th>ip地址</th>
<th>城市</th>
<th>编码</th>
<th>扫描商品</th>
<th>批号</th>
<th>时间</th>
<th class="hidden-480">操作</th>
</tr>
</thead>
{if condition="!empty($list)"}
{foreach $list as $vl}
<tbody>

<tr>
<td>{$vl['num']}</td>
<td>{$vl['username']}</td>
{if condition="($vl['sex'] == 1)"}
<td>男</td>
{elseif condition="($vl['sex'] == 2)" /}
<td>女</td>
{else /}
<td>未知</td>
{/if}
<td>{$vl['ip']}</td>
<td>{$vl['city']}</td>
<td>{$vl['sn']}</td>
<td>{$vl['con']}</td>
<td>{$vl['ph']}</td>
<td><?php echo date('Y-m-d H-i-s',$vl['time']); ?></td>
<td>
<div class="hidden-sm hidden-xs btn-group">
<a class="btn btn-xs btn-danger" href="{:url('Jgymuser/delinfo',array('id'=>$vl['cid']))}">
<i class="ace-icon fa fa-trash-o bigger-120"></i>
</a>
</div>
</td>
</tr>

</tbody>
{/foreach}
{/if}
</table>
{$pagefoot}
</div><!-- /.span -->
</div><!-- /.row -->
<div style="text-align: center;">

</div>
</div><!-- /.col -->
</div><!-- /.row -->
</div><!-- /.page-content-area -->

</div>

2:控制器里

//展示

public function saomiao(){

$w="";
if(input("date-range-picker")){
$time = input("date-range-picker");
$time = explode('-',$time);
$time[0] = str_replace("/","-",$time[0]);
$time[0] = explode('-',$time[0]);
$start = trim($time[0][2])."-".trim($time[0][0])."-".trim($time[0][1])." 00:00:01";
$time[1] = str_replace("/","-",$time[1]);
$time[1] = explode('-',$time[1]);
$end = trim($time[1][2])."-".trim($time[1][0])."-".trim($time[1][1])." 23:59:59";
$start = strtotime($start);
$end = strtotime($end);
$w['time']  = ['between',[$start,$end]];
if(input("username")){
$w['username']=input("username");
}
if(input("city")){
$w['city']=input("city");
}
if(input("ph")){
$w['ph']=input("ph");
}
if(input("con")){
$w['con']=input("con");
}
if(input("ip")){
$w['ip']=input("ip");
}


$list = Db::name('user')->where($w)->order("cid desc")->paginate(10,false,['query'=>request()->param()]);
$pagefoot=$list->render();
}else{
if(input("username")){
$w['username']=input("username");
}
if(input("city")){
$w['city']=input("city");
}
if(input("ph")){
$w['ph']=input("ph");
}
if(input("con")){
$w['con']=input("con");
}
if(input("ip")){
$w['ip']=input("ip");
}


// [
// 'type'     => 'Bootstrap',
// 'var_page' => 'page',
// ]
//$list = Db::name('user')->where($w)->order('cid desc')->select();
//print_r($list);die;
$list = Db::name('user')->where($w)->order("cid desc")->paginate(10,false,['query'=>request()->param()]);

$pagefoot=$list->render();
//print_r($list);die;['query'=>request()->param()]



}




//}

$this->assign('username', input("username"));
$this->assign('city', input("city"));
$this->assign('ph', input("ph"));
$this->assign('con', input("con"));
$this->assign('ip', input("ip"));
$this->assign('list', $list);
$this->assign('pagefoot', $pagefoot);
return $this->fetch('jguser');

}

//excel导出
public function Excelout(){
$w="";
if(input("date-range-picker")){
$time = input("date-range-picker");
$time = explode('-',$time);
$time[0] = str_replace("/","-",$time[0]);
$time[0] = explode('-',$time[0]);
$start = trim($time[0][2])."-".trim($time[0][0])."-".trim($time[0][1])." 00:00:01";
$time[1] = str_replace("/","-",$time[1]);
$time[1] = explode('-',$time[1]);
$end = trim($time[1][2])."-".trim($time[1][0])."-".trim($time[1][1])." 23:59:59";
$start = strtotime($start);
$end = strtotime($end);
$w['time']  = ['between',[$start,$end]];
if(input("username")){
$w['username']=input("username");
}
if(input("city")){
$w['city']=input("city");
}
if(input("ph")){
$w['ph']=input("ph");
}
if(input("con")){
$w['con']=input("con");
}
if(input("ip")){
$w['ip']=input("ip");
}
}else{
if(input("username")){
$w['username']=input("username");
}
if(input("city")){
$w['city']=input("city");
}
if(input("ph")){
$w['ph']=input("ph");
}
if(input("con")){
$w['con']=input("con");
}
if(input("ip")){
$w['ip']=input("ip");
}


}
//print_r($w);die;
$list = Db::name('user')->where($w)->order('cid desc')->select();
// print_r($list);die;
import('PHPExcel.PHPExcel', EXTEND_PATH);
        $phpexcel=new \PHPExcel();//实例化PHPExcel类对象,方便操作即将生成的excel表格
        $phpexcel->setActiveSheetIndex(0);//选中我们生成的excel文件的第一张工作表
        $sheet=$phpexcel->getActiveSheet();//获取到选中的工作表,方面后面数据插入操作
        //此处设置的是生成的excel表的第一行标题
       
         //将我们上面手动设置的标题信息放到数组中,作为第一行写入数据表
        $currow=0;//因为我们生成的excel表格的行数是从1开始,所以我们预先设置好一个变量,供下面foreach循环的$k使用
       
      $sheet->setCellValue('A1','扫描次数')
                  ->setCellValue('B1','微信昵称')
                  ->setCellValue('C1','性别')
                  ->setCellValue('D1','ip地址')
              ->setCellValue('E1','城市')
                  ->setCellValue('F1','编码')
                  ->setCellValue('G1','扫描商品')
              ->setCellValue('H1','批号')
              ->setCellValue('I1','时间')
                  
            ;


            foreach ($list as $k => $v) {
        if($v['sex']==1){
                $v['sex']="男";
            }else{
                $v['sex']="女";
            }
            $v['time']=date("Y-m-d H:i:s",$v['time']);
        $currow=$k+1;//表示从生成的excel表格的第一行开始插入数据
            $sheet->setCellValue('A'.$currow,$v['num'])
                  ->setCellValue('B'.$currow,$v['username'])
                  ->setCellValue('C'.$currow,$v['sex'])
                  ->setCellValue('D'.$currow,$v['ip'])
              ->setCellValue('E'.$currow,$v['city'])
                  ->setCellValue('F'.$currow,$v['sn'])
                  ->setCellValue('G'.$currow,$v['con'])
              ->setCellValue('H'.$currow,$v['ph'])
                  ->setCellValue('I'.$currow,$v['time'])
                  
            ;
        }
        header('Content-Type:application/octet-stream');
        header('Accept-Ranges:bytes');
        header('Content-Type: application/vnd.ms-excel');//设置下载前的头信息
        header('Content-Disposition: attachment;filename="扫描记录表.xlsx"');
        header('ache-Control: max-age=0');
        header('Pragma: no-cache');
        header('Expires: 0');
        $phpwriter=new \PHPExcel_Writer_Excel2007($phpexcel);
        //此处的2007代表的是高版本的excel表格
        $phpwriter->save('php://output');//生成并下载excel表格
        return;


}


猜你喜欢

转载自blog.csdn.net/tianjingang1/article/details/80977005