Example #1
0
 /**
  * 数据库分页
  * @example  DB::w()->from(table)->page(['url'=>url,'page'=>10,'class'=>'pagination','count'=>'count(*) num']);  
  * @param array $condition   
  * @return 数组 ['data','pages']
  */
 function page($condition = [])
 {
     $url = $condition['url'];
     $count = $condition['count'] ?: "count(*) num";
     $class = $condition['class'] ?: "pagination";
     $per_page = $condition['per_page'];
     if (!$per_page) {
         $per_page = $condition['page'] ?: 10;
     }
     unset($condition['url'], $condition['per_page'], $condition['page'], $condition['class']);
     if ($condition) {
         foreach ($condition as $conditionkey => $conditionValue) {
             if (strpos($conditionkey, 'where') !== false) {
                 $this->{$conditionkey}($conditionValue[0], $conditionValue[1]);
             } else {
                 $this->{$conditionkey}($conditionValue);
             }
         }
     }
     $this->count($count);
     $this->_query(true);
     $query = $this->query;
     $row = $query->fetch(PDO::FETCH_OBJ);
     $paginate = new Paginate($row->num, $per_page);
     $paginate->url = $url;
     $limit = $paginate->limit;
     $offset = $paginate->offset;
     //显示分页条,直接输出 $paginate
     $pages = $paginate->show($class);
     if ($condition) {
         foreach ($condition as $conditionkey => $conditionValue) {
             if (strpos($conditionkey, 'where') !== false) {
                 $this->{$conditionkey}($conditionValue[0], $conditionValue[1]);
             } else {
                 $this->{$conditionkey}($conditionValue);
             }
         }
     }
     $this->limit($limit);
     $this->offset($offset);
     $this->_query(true);
     $posts = $this->query->fetchAll();
     unset($this->ar, $this->where);
     return (object) ['data' => $posts, 'pager' => $pages, 'count' => $row->num];
 }