自定义函数库
<?php namespace app\common\fun; class DiyData{ /** * 获取某张表数据 使用方法 fun('diydata@getData',['fid'=>5]) * $table 数据库表名 * $where 条件 数组形式 */ public function getData($table = null,$where = []){ if($table == null){ return ''; } $data = db($table) -> where($where) -> select(); return $data; } /** * 获取针对方向的书 * $table 数据库表名 * $strid 条件 id字符串 * $rows 显示条数 */ public function getfangxiang($table=null, $strid='', $rows=20){ if($table == null){ return ''; } if(empty($strid)){ return ''; } $where['id'] = array('in', $strid ); $data = db($table) -> where($where) ->limit($rows)-> select() ; return $data; } /* * 获取筛选条件url数据 * $data 二维数组 * */ public function getUrlData($k = null){ $data = request() -> get(); if(!empty($k)){ unset($data[$k]); } $whereStr = ''; if(!empty($data)){ foreach($data as $key => $val){ if($val == '' || $key == 'page'){ continue; } $whereStr .= $key.'='.$val; if($val != end($data)){ $whereStr .= '&'; } } } return $whereStr; } /* * 选中筛选条件 * $id 选项ID * $k 类型关键字 * */ public function checked($id,$k){ $data = request() -> get(); $str = ''; if($data[$k] == $id){ $str = 'hover'; } return $str; } /* * * 获取数据 * $table 数据表名 * $where 条件 数组形式 * $pagesize 显示数量 * $order 排序方式 * */ public function getList($table,$where,$pagesize = 10, $order = 'view desc'){ $page = empty(input("page")) ? 1 : input("page"); $page = ($page - 1) * $pagesize; $where = $this -> strToArr($where); if(!empty($where['title'])){ $where['title'] = array("like", "%{$where['title']}%"); } //学习目标 $wheremb = 1; if(!empty($where['learning_objectives'])){ $wheremb = "FIND_IN_SET({$where['learning_objectives']}, learning_objectives)" ; unset($where['learning_objectives']); } //针对方向 $wherefx = 1; if(!empty($where['direction_oriented'])){ $wherefx = "FIND_IN_SET({$where['direction_oriented']}, direction_oriented)" ; unset($where['direction_oriented']); } //针对院校 $whereyx = 1; if(!empty($where['universities_colleges'])){ $whereyx = "FIND_IN_SET({$where['universities_colleges']}, universities_colleges)" ; unset($where['universities_colleges']); } //整期收费 $wheresf = 1; if(!empty($where['cost'])){ $wheresf = "FIND_IN_SET({$where['cost']}, cost)" ; unset($where['cost']); } if(!empty($where['orderby'])){ if($where['orderby'] == 'asc'){ $order = "view asc"; } unset($where['orderby']); } $data = db($table) -> where($where) -> where($wheremb)-> where($wherefx)-> where($whereyx)-> where($wheresf)-> limit($page,$pagesize) -> order($order) -> select(); return $data; } /* * * 获取分页数据 * $table 数据表名 * $where 条件 数组形式 * $pagesize 显示数量 * */ public function getPage($table,$where,$pagesize = 10){ $page = empty(input("page")) ? 1 : input("page"); $where = $this -> strToArr($where); if(!empty($where['title'])){ $where['title'] = array("like", "%{$where['title']}%"); } //学习目标 $wheremb = 1; if(!empty($where['learning_objectives'])){ $wheremb = "FIND_IN_SET({$where['learning_objectives']}, learning_objectives)" ; unset($where['learning_objectives']); } //针对方向 $wherefx = 1; if(!empty($where['direction_oriented'])){ $wherefx = "FIND_IN_SET({$where['direction_oriented']}, direction_oriented)" ; unset($where['direction_oriented']); } //针对院校 $whereyx = 1; if(!empty($where['universities_colleges'])){ $whereyx = "FIND_IN_SET({$where['universities_colleges']}, universities_colleges)" ; unset($where['universities_colleges']); } //整期收费 $wheresf = 1; if(!empty($where['cost'])){ $wheresf = "FIND_IN_SET({$where['cost']}, cost)" ; unset($where['cost']); } if(!empty($where['orderby'])){ unset($where['orderby']); } $total = db($table) -> where($where) -> where($wheremb)-> where($wherefx)-> where($whereyx)-> where($wheresf)-> count(); $totalPage = ceil($total / $pagesize); if($total <= 0){ return ''; } $pageHtml = ''; /* * 限制分页条数 start * author:Abner * time:2019.9.25 * */ $starPage = $page - 5 ; if($page <= 5){ $starPage = 1; } $endPage = $page + 5; if($endPage >= $totalPage) { $endPage = $totalPage; } /* * 限制分页条数 end * */ for($i = $starPage; $i <= $endPage; $i++){ $pageHtml .= '<li '.($i == $page ? 'id="num"' : '').'><a href="?page='.$i.'&'.$this -> getUrlData().'">'.$i.'</a></li>'; } $previousPage = '?page='.(($page - 1) <= 0 ? 1 : $page -1).'&'.$this -> getUrlData(); //上一页链接 $nextPage = '?page='.(($page + 1) >= $totalPage ? $totalPage : $page + 1).'&'.$this -> getUrlData(); //下一页链接 $pageHtml .= '<li><a href="'.$previousPage.'">上一页</a></li> <li>到第<input id="jumpPage" type="number" class="ye"/>页</li> <li><a href="'.$nextPage.'">下一页</a></li>'; $minPage = 1; $maxPage = $totalPage; $url = '&'.$this -> getUrlData(); //跳转链接 $pageHtml .= <<<EOD <script> $("#jumpPage").on("change",function(r){ var page = r.target.value; if(page <= {$minPage}){ page = {$minPage}; } if(page >= {$maxPage}){ page = {$maxPage}; } window.location.href="?page="+page+"{$url}"; }); </script> EOD; return $pageHtml; } /* * 将字符串转换为数组 * $str 字符串 * $delimiter 分隔符 * */ public function strToArr($str = '',$delimiter = '&'){ if(empty($str)){ return ''; } $arr = explode($delimiter,$str); if($delimiter != '&'){ return $arr; } $dataArr = []; foreach ($arr as $k => $v){ $data = explode("=",$v); $dataArr[$data['0']] = $data['1']; } return $dataArr; } /* * 将关键字转换为HTml格式 * $str 字符串 * $labelStart 标签前半部 * $labelEnd 标签后半部 * */ public function keyToHtml($str = '',$labelStart = '',$labelEnd = ''){ if(empty($str)){ return ''; } $Arr = explode(',',$str); $dataStr = ''; foreach ($Arr as $v){ $dataStr .= $labelStart.$v.$labelEnd; } return $dataStr; } /* * 获取排行榜来源 * $table 数据表 * $type 查询类型 find:单条记录、select:多条记录 * $where 查询条件 数组形式,例:['id'=>5] * $fields 返回字段 * */ public function getDiyData($table = '',$where = [],$type = 'find',$fields = '*'){ if(empty($table)){ return ''; } $data = db($table) -> field($fields) -> where($where) -> $type(); return $data; } /* * 获取点赞状态 * $table 数据表 * $where 查询条件 数组形式,例:['id'=>5] * return bool 返回布尔 * * */ public function getCollectState($table = '',$aid){ $where=[ 'uid'=>login_user('uid'), 'aid'=>$aid ]; $data = db($table) -> field('id') -> where($where) -> find(); if($data){ return true; } return false; } }