用户
 找回密码
 入住 CI 中国社区
搜索
查看: 5399|回复: 8
收起左侧

[库 Library] digg分页

[复制链接]
发表于 2011-2-10 14:47:03 | 显示全部楼层 |阅读模式
本帖最后由 jackiesun 于 2011-3-21 16:47 编辑

发现有朋友在使用的时候 提到无法使用,现发上源码。


tuangou.rar (381.74 KB, 下载次数: 83)
分页效果与digg差不多。 未命名.jpg 使用方法:
PHP复制代码
 
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下

PHP复制代码
<?php  if ( ! 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
PHP复制代码
 
 
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;
}
 
 
复制代码

评分

参与人数 1威望 +5 收起 理由
Hex + 5 原创内容

查看全部评分

发表于 2011-3-4 17:51:25 | 显示全部楼层
有显示效果,但是点击的页码的时候不能显示内容.希望楼主贴出循环内容的代码(views)
发表于 2011-3-4 17:52:54 | 显示全部楼层
<? 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>
为什么不管用呢?
 楼主| 发表于 2011-3-21 16:46:18 | 显示全部楼层
本地测试过了,没有问题。
PHP写的规范一点
PHP复制代码
<?php  ?>
复制代码
发表于 2011-4-19 15:19:20 | 显示全部楼层
A PHP Error was encountered
Severity: Notice

Message: Undefined index: page

Filename: libraries/pager.php

Line Number: 266



为什么我点除了第一页其他也回报这个错误
发表于 2011-4-20 13:06:58 | 显示全部楼层
学习中,,支持了
发表于 2011-4-20 13:31:27 | 显示全部楼层
如何倒叙输出呢
发表于 2011-6-13 14:14:46 | 显示全部楼层
为什么我的
...这些符号没有呢?
发表于 2012-6-12 11:27:42 | 显示全部楼层
为什么会在2.1版本里面我用get_where就不可以了呢?加个条件就不行啊

本版积分规则