Heim php教程 PHP源码 ThinkPHP抽出来的分页类

ThinkPHP抽出来的分页类

May 25, 2016 pm 05:12 PM


代码片段

                       

                       

1. [图片] default_theme.jpg    

           

ThinkPHP抽出来的分页类

                                               

                       

2. [图片] theme_1.jpg    

           

ThinkPHP抽出来的分页类

                                               

                               

3. [文件]     demo_ajax.php ~ 805B          


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<?php

require_once 'mysql.class.php';

require_once 'page.class.php';

require_once 'config.php';

 

$mysql = new MySQL(DB_HOST_NAME,DB_USER_NAME,DB_PASS_WORD,DB_DATABASE_NAME);

 

$pagenum = isset ( $_REQUEST ['page'] ) ? $_REQUEST ['page'] : 1;

$page_row = 5;//每页显示条数

$count = $mysql->count ( "select id from ajax" );//总数

 

$p = new Page ($count,$page_row,7);

 

$p->AjaxType = true;

$p->isJumpPage = true;

//$p->function_name = 'yourself';

$p->setConfig('theme','%totalRow% %header% %nowPage%/%totalPage% 页%first% %upPage% %linkPage% %downPage% %end% %jump%');

 

$sql = "select * from ajax limit ".($pagenum-1)*$page_row.",".$page_row;

$result = $mysql->query ( $sql );

 

$return_data['data'] = $result;

$return_data['page'] = $p->show();

 

echo json_encode($return_data);

Nach dem Login kopieren

4. [文件] page.class.php ~ 7KB

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

<?php

// +----------------------------------------------------------------------

// |分页类

// +----------------------------------------------------------------------

// | Author: justmepzy(justmepzy@gmail.com)

// +----------------------------------------------------------------------

class Page{

    // 起始行数

    public $firstRow;

    // 列表每页显示行数

    public $listRows;

    // 分页总页面数

    protected $totalPages;

    // 总行数

    protected $totalRows;

    // 当前页数

    protected $nowPage;

    // 分页的栏的总页数

    protected $coolPages;

    // 分页栏每页显示的页数

    protected $rollPage;

    //是否显示linkpage

    protected $isShowLinkPage = FALSE;

    //是否显示输入页数跳转

    public $isJumpPage = FALSE;

    //是否使用Ajax,默认不使用

    public $AjaxType = FALSE;

    //JavaScript中使用的获取数据方法名

    public $function_name = 'ajax_page';

    // 分页显示定制

    protected $config = array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'首页','last'=>'末页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end% %jump%');

    /**

     +----------------------------------------------------------

     *

     +----------------------------------------------------------

     * @access public

     +----------------------------------------------------------

     * @param array $totalRows  总的记录数

     * @param array $listRows  每页显示记录数

     * @param array $rollpage  是否显示linkpage条数

     +----------------------------------------------------------

     */

    public function __construct($totalRows,$listRows,$rollpage){

        $this->totalRows = $totalRows;

        $this->listRows = $listRows;

        $this->rollPage = $rollpage;

        $this->listRows = !empty($listRows)?$listRows:10;

        $this->isShowLinkPage = empty($rollpage)?false:true;

        $this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数

        $this->coolPages  = ceil($this->totalPages/$this->rollPage);

        $this->nowPage  = !empty($_REQUEST['page'])?$_REQUEST['page']:1;

        if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {

            $this->nowPage = $this->totalPages;

        }

        $this->firstRow = $this->listRows*($this->nowPage-1);

         

    }

    /**

     +----------------------------------------------------------

     * 设置显示样式

     +----------------------------------------------------------

     * @access public

     +----------------------------------------------------------

     */

    public function setConfig($name,$value) {

        if(isset($this->config[$name])) {

            $this->config[$name]    =   $value;

        }

    }

    /**

     +----------------------------------------------------------

     * 分页显示输出

     +----------------------------------------------------------

     * @access public

     +----------------------------------------------------------

     */

    public function show() {

        if(0 == $this->totalRows) return '';

        $p = 'page';

        $nowCoolPage      = ceil($this->nowPage/$this->rollPage);

        $request_url = $_SERVER['REQUEST_URI'];

         

        if(strpos($request_url,'?')){

            $url  $request_url;

        }else{

            $url  $request_url.'?';

        }

         

        $parse = parse_url($url);

        if(isset($parse['query'])) {

            parse_str($parse['query'],$params);

            unset($params[$p]);

            $url   $parse['path'].'?'.http_build_query($params);

        }

         

        //上下翻页字符串

        $upRow   = $this->nowPage-1;

        $downRow = $this->nowPage+1;

        if ($upRow>0){        

            if($this->AjaxType)

                $upPage='<a href=javascript:'.$this->function_name.'('.$upRow.');>'.$this->config['prev'].'</a>';//ajax方式

            else

                $upPage='<a href=\''.$url.'&'.$p.'='.$upRow.'\'>'.$this->config['prev'].'</a>';

        }else{

            $upPage='';

        }

 

        if ($downRow <= $this->totalPages){

            if($this->AjaxType)

                $downPage='<a href=javascript:'.$this->function_name.'('.$downRow.');>'.$this->config['next'].'</a>';//ajax方式

            else

                $downPage='<a href=\''.$url.'&'.$p.'='.$downRow.'\'>'.$this->config['next'].'</a>';

        }else{

            $downPage='';

        }

        // << < > >>

        if($this->nowPage == 1){

            $theFirst = '';

            $prePage = '';

        }else{

            $preRow $this->nowPage-$this->rollPage;

            if($this->AjaxType)

                $theFirst = '<a href=javascript:'.$this->function_name.'(1);>'.$this->config['first'].'</a>';//ajax方式

            else

                $theFirst = '<a href=\''.$url.'&'.$p.'=1\' >'.$this->config['first'].'</a>';

        }

        if($this->nowPage == $this->totalPages){

            $nextPage = '';

            $theEnd='';

        }else{

            $nextRow = $this->nowPage+$this->rollPage;

            $theEndRow = $this->totalPages;

            if($this->AjaxType)

                $theEnd = '<a href=javascript:'.$this->function_name.'('.$theEndRow.');>'.$this->config['last'].'</a>';//ajax方式

            else

                $theEnd = '<a href=\''.$url.'&'.$p.'='.$theEndRow.'\' >'.$this->config['last'].'</a>';

        }

        // 1 2 3 4 5

        if($this->isShowLinkPage){

            $linkPage = '';

            for($i=1;$i<=$this->rollPage;$i++){

            $page=($nowCoolPage-1)*$this->rollPage+$i;

            //保证linkpage保持当前页不是最后一个

            if($this->nowPage%$this->rollPage ==0 ){

                $page = ($nowCoolPage-1)*$this->rollPage+$i+$this->rollPage-1;

            }

            if($page!=$this->nowPage){

                if($page<=$this->totalPages){

                    if($this->AjaxType)

                        $linkPage .= ' <a href=javascript:'.$this->function_name.'('.$page.');> '.$page.' </a>';//ajax方式

                    else

                        $linkPage .= ' <a href=\''.$url.'&'.$p.'='.$page.'\'> '.$page.' </a>';

                }else{

                    break;

                }

            }else{

                if($this->totalPages != 1){

                    $linkPage .= ' <span class=\'currentpage\'>'.$page.'</span>';

                }

            }

          }

        }

        //jump page

        if($this->isJumpPage){

            $jump = '<input type=\'text\' id=\'jumppage\' value = \''.$this->nowPage.'\'>';

            $jump .='<input type=\'button\' id=\'jumppagebutton\' value=\'GO\' onclick=\'go_page('.$this->nowPage.')\'>';

            $jump .='<script>function go_page(page){var jumptopage = document.getElementById(\'jumppage\').value;var topage;';

            $jump .='if(jumptopage==page||isNaN(jumptopage)){return false;}else{ ';

            $jump .='if(jumptopage>'.$this->totalPages.'){topage = '.$this->totalPages.'}';

            $jump .='else if(jumptopage<1){topage = 1;}else{topage = jumptopage;}';

            if($this->AjaxType){

                $jump .= $this->function_name.'(topage);';

            }else{

                $jump .='window.location.href = \''.$url.'&'.$p.'=\'+topage;';

            }

            $jump .='}}</script>';

        }

        $pageStr=str_replace(

        array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%','%jump%'),

        array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd,$jump),$this->config['theme']);

        return $pageStr;

    }

     

}

Nach dem Login kopieren

                               

                   

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße Artikel -Tags

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)