digg分页
本帖最后由 jackiesun 于 2011-3-21 16:47 编辑发现有朋友在使用的时候 提到无法使用,现发上源码。
分页效果与digg差不多。 使用方法:
function index()
{
//加载分页类,自已写的
$this->load->library('pager');
$list = $this->pager->init('users',1)->ar();
//取得地址上的第三个参数
$current = $this->uri->segment(3);
$this->load->view('welcome_message',array(
'query'=>$list->query,
'current'=>$current
)
);
}
只有一个文件
Pager.php 放到 libraries下
<?phpif ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* 分页类实现 digg分页效果
*
* @package
libraries
* @author
轻飘如羽 [yiiphp@gmail.com]
* @copyright
Copyright (c) 2011
* @license
http://codeigniter.com/user_guide/license.html
* @link
http://www.fowcms.com
使用方法:
控制器中代码:controllers目录
//加载分页类,自已写的
$this->load->library('pager');
$list = $this->pager->init('users',5)->ar();
//取得地址上的第三个参数
$current = $this->uri->segment(3);
$this->load->view('welcome_message',array(
'query'=>$list->query,
'current'=>$current
)
);
视图中代码。views目录
$this->pager->digg('welcome/index',$current);
*/
class Pager {
public $length = 5;
public $prevLabel = '';
public $nextLabel = '';
public $first = 0; //第一页
public $slider = 3;
public $perpage = 3;
public $query;
public $table;
public $count;
public $obj;
public $current;
/*function __construct($params=array()){
} */
function init($table='users',$perpage=2,$segment=3){
$this->table = $table;
//取得ci超级对象
$this->obj =& get_instance();
//加载数据库类
$this->obj->load->database();
//取得地址上的第三个参数
$this->current = $this->obj->uri->segment($segment);
$this->perpage = $perpage;
return $this;
}
//使用AR
function ar(){
$this->count = $this->obj->db->count_all($this->table);
$this->query = $this->obj->db->get($this->table, $this->perpage,$this->perpage*$this->current);
return $this;
}
//使用 自己写的SQL
function sql($query,$count){
$this->count = $count;
$this->query = $query;
return $this;
}
/**
* 读取配置信息,或修改配置信息
*
* @param 链接$url
* @param 当前页 $current
* @param 总页数 $all_count
* @param 每页显示数 $perpage
* @return unknown
*/
function digg($url,$current){
$perpage = $this->perpage;
$last = ceil($this->count/$perpage);
$prev = $current - 1; //上一页
$next = $current + 1; //下一页
$output = "<ul class='pagenav'>";
if ($current == $this->first) {
$output .= "<li class=\"disabled\">« ".$this->prevLabel."</li>\n";
} else {
$output .= "<li><a href='"._url($url)."'>«".$this->prevLabel."</a></li>\n";
}
$mid = intval($this->length / 2);
if ($current < $this->first) {
$current = $this->first;
}
if ($current > $last) {
$current = $last;
}
$begin = $current - $mid;
if ($begin < $this->first) { $begin = $this->first; }
$end = $begin + $this->length - 1;
if ($end >= $last) {
$end = $last-1;
$begin = $end - $this->length + 1;
if ($begin < $this->first) { $begin = $this->first; }
}
if ($begin > $this->first) {
for ($i = $this->first; $i < $this->first + $this->slider && $i < $begin; $i++) {
$page= $i;
$in = $i + 1;
$urls = _url($url,array('page'=>$page));
$output .= "<li><a href=\"{$urls}\">{$in}</a></li>\n";
}
if ($i < $begin) {
$output .= "<li class=\"none\">...</li>\n";
}
}
for ($i = $begin; $i <= $end ; $i++) {
$page = $i;
$in = $i + 1;
if ($i == $current) {
$output .= "<li class=\"current\">{$in}</li>\n";
} else {
$urls = _url($url,array('page'=>$page));
$output .= "<li><a href=\"{$urls}\">{$in}</a></li>\n";
}
}
if ($last - $end > $this->slider) {
$output .= "<li class=\"none\">...</li>\n";
$end = $last - $this->slider;
}
for ($i = $end + 1; $i < $last; $i++) {
$page = $i;
$in = $i + 1;
$urls = _url($url,array('page'=>$page));
$output .= "<li><a href=\"{$urls}\">{$in}</a></li>\n";
}
if ($current == $last-1) {
$output .= "<li class=\"disabled\">".$this->nextLabel." »</li>\n";
} else {
$page = $last-1;
$urls = _url($url,array('page'=>$page));
$output .= "<li><a href=\"{$urls}\">".$this->nextLabel." »</a></li>\n";
}
$output .= "</ul>\n";
echo $output;
}
}
//修改该函数可以放到其他框架使用
function _url($url,$array = array()){
return site_url($url.'/'.$array['page']);
}
css
ul.pagenav {
font-size: 12px;
font-weight: bold;
list-style: none;
margin: 0px;
padding: 0px;
}
.pagenav li {
list-style: none;
background-color: #fff;
margin: 0px;
display: block;
float: left;
margin-left: 2px;
margin-right: 2px;
}
.pagenav li.disabled {
border: 1px solid #DDDDDD;
padding: 2px 6px 2px 6px;
color: #ccc;
}
.pagenav li.current {
border: 1px solid #2E6AB1;
padding: 2px 6px 2px 6px;
background-color: #2E6AB1;
color: #fff;
}
.pagenav li.none {
border: 1px none;
padding: 2px 6px 2px 6px;
}
.pagenav li a {
border: 1px solid #9AAFE5;
padding: 2px 6px 2px 6px;
display: block;
text-decoration: none;
color: #105CB6;
}
.pagenav li a:hover {
border: 1px solid #2E6AB1;
color: #000;
}
有显示效果,但是点击的页码的时候不能显示内容.希望楼主贴出循环内容的代码(views) <? foreach($query->result() as $row):?>
<tr>
<td><?=$row->title?></td>
<td><?=$row->author?></td>
<td><?=$row->sendtime?></td>
</tr>
<? endforeach;?>
<tr>
<td><?echo $this->pager->digg('admin/news/index',$current);?></td>
为什么不管用呢? 本地测试过了,没有问题。
PHP写的规范一点
<?php?> A PHP Error was encountered
Severity: Notice
Message: Undefined index: page
Filename: libraries/pager.php
Line Number: 266
为什么我点除了第一页其他也回报这个错误 学习中,,支持了 如何倒叙输出呢 为什么我的
...这些符号没有呢?
为什么会在2.1版本里面我用get_where就不可以了呢?加个条件就不行啊
页:
[1]