Heim > php教程 > PHP源码 > PHP 分页类

PHP 分页类

PHP中文网
Freigeben: 2016-05-25 16:59:55
Original
1388 Leute haben es durchsucht

PHP 分页类

PHP 分页类

PHP 分页类

分页代码

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

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

<?php

/**

 * 分页类

 * @author  xiaojiong & 290747680@qq.com

 * @date 2011-08-17

 *

 * show(2)  1 ... 62 63 64 65 66 67 68 ... 150

 * 分页样式

 * #page{font:12px/16px arial}

 * #page span{float:left;margin:0px 3px;}

 * #page a{float:left;margin:0 3px;border:1px solid #ddd;padding:3px 7px; text-decoration:none;color:#666}

 * #page a.now_page,#page a:hover{color:#fff;background:#05c}

*/

  

class Core_Lib_Page

{

    public     $first_row;        //起始行数

  

    public     $list_rows;        //列表每页显示行数

      

    protected  $total_pages;      //总页数

  

    protected  $total_rows;       //总行数

      

    protected  $now_page;         //当前页数

      

    protected  $method  = &#39;defalut&#39;; //处理情况 Ajax分页 Html分页(静态化时) 普通get方式

      

    protected  $parameter = &#39;&#39;;

      

    protected  $page_name;        //分页参数的名称

      

    protected  $ajax_func_name;

      

    public     $plus = 3;         //分页偏移量

      

    protected  $url;

      

      

    /**

     * 构造函数

     * @param unknown_type $data

     */

    public function __construct($data = array())

    {

        $this->total_rows = $data[&#39;total_rows&#39;];

  

        $this->parameter         = !empty($data[&#39;parameter&#39;]) ? $data[&#39;parameter&#39;] : &#39;&#39;;

        $this->list_rows         = !empty($data[&#39;list_rows&#39;]) && $data[&#39;list_rows&#39;] <= 100 ? $data[&#39;list_rows&#39;] : 15;

        $this->total_pages       = ceil($this->total_rows / $this->list_rows);

        $this->page_name         = !empty($data[&#39;page_name&#39;]) ? $data[&#39;page_name&#39;] : &#39;p&#39;;

        $this->ajax_func_name    = !empty($data[&#39;ajax_func_name&#39;]) ? $data[&#39;ajax_func_name&#39;] : &#39;&#39;;

          

        $this->method           = !empty($data[&#39;method&#39;]) ? $data[&#39;method&#39;] : &#39;&#39;;

          

          

        /* 当前页面 */

        if(!empty($data[&#39;now_page&#39;]))

        {

            $this->now_page = intval($data[&#39;now_page&#39;]);

        }else{

            $this->now_page   = !empty($_GET[$this->page_name]) ? intval($_GET[$this->page_name]):1;

        }

        $this->now_page   = $this->now_page <= 0 ? 1 : $this->now_page;

      

          

        if(!empty($this->total_pages) && $this->now_page > $this->total_pages)

        {

            $this->now_page = $this->total_pages;

        }

        $this->first_row = $this->list_rows * ($this->now_page - 1);

    }  

      

    /**

     * 得到当前连接

     * @param $page

     * @param $text

     * @return string

     */

    protected function _get_link($page,$text)

    {

        switch ($this->method) {

            case &#39;ajax&#39;:

                $parameter = &#39;&#39;;

                if($this->parameter)

                {

                    $parameter = &#39;,&#39;.$this->parameter;

                }

                return &#39;<a onclick="&#39; . $this->ajax_func_name . &#39;(\&#39;&#39; . $page . &#39;\&#39;&#39;.$parameter.&#39;)" href="javascript:void(0)">&#39; . $text . &#39;</a>&#39; . "\n";

            break;

              

            case &#39;html&#39;:

                $url = str_replace(&#39;?&#39;, $page,$this->parameter);

                return &#39;<a href="&#39; .$url . &#39;">&#39; . $text . &#39;</a>&#39; . "\n";

            break;

              

            default:

                return &#39;<a href="&#39; . $this->_get_url($page) . &#39;">&#39; . $text . &#39;</a>&#39; . "\n";

            break;

        }

    }

      

      

    /**

     * 设置当前页面链接

     */

    protected function _set_url()

    {

        $url  $_SERVER[&#39;REQUEST_URI&#39;].(strpos($_SERVER[&#39;REQUEST_URI&#39;],&#39;?&#39;)?&#39;&#39;:"?").$this->parameter;

        $parse = parse_url($url);

        if(isset($parse[&#39;query&#39;])) {

            parse_str($parse[&#39;query&#39;],$params);

            unset($params[$this->page_name]);

            $url   $parse[&#39;path&#39;].&#39;?&#39;.http_build_query($params);

        }

        if(!empty($params))

        {

            $url .= &#39;&&#39;;

        }

        $this->url = $url;

    }

      

    /**

     * 得到$page的url

     * @param $page 页面

     * @return string

     */

    protected function _get_url($page)

    {

        if($this->url === NULL)

        {

            $this->_set_url();  

        }

    //  $lable = strpos(&#39;&&#39;, $this->url) === FALSE ? &#39;&#39; : &#39;&&#39;;

        return $this->url . $this->page_name . &#39;=&#39; . $page;

    }

      

      

    /**

     * 得到第一页

     * @return string

     */

    public function first_page($name = &#39;第一页&#39;)

    {

        if($this->now_page > 5)

        {

            return $this->_get_link(&#39;1&#39;, $name);

        }  

        return &#39;&#39;;

    }

      

    /**

     * 最后一页

     * @param $name

     * @return string

     */

    public function last_page($name = &#39;最后一页&#39;)

    {

        if($this->now_page < $this->total_pages - 5)

        {

            return $this->_get_link($this->total_pages, $name);

        }  

        return &#39;&#39;;

    

      

    /**

     * 上一页

     * @return string

     */

    public function up_page($name = &#39;上一页&#39;)

    {

        if($this->now_page != 1)

        {

            return $this->_get_link($this->now_page - 1, $name);

        }

        return &#39;&#39;;

    }

      

    /**

     * 下一页

     * @return string

     */

    public function down_page($name = &#39;下一页&#39;)

    {

        if($this->now_page < $this->total_pages)

        {

            return $this->_get_link($this->now_page + 1, $name);

        }

        return &#39;&#39;;

    }

  

    /**

     * 分页样式输出

     * @param $param

     * @return string

     */

    public function show($param = 1)

    {

        if($this->total_rows < 1)

        {

            return &#39;&#39;;

        }

          

        $className = &#39;show_&#39; . $param;

          

        $classNames = get_class_methods($this);

  

        if(in_array($className, $classNames))

        {

            return $this->$className();

        }

        return &#39;&#39;;

    }

      

    protected function show_2()

    {

        if($this->total_pages != 1)

        {

            $return = &#39;&#39;;

            $return .= $this->up_page(&#39;<&#39;);

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

            {

                if($i == $this->now_page)

                {

                    $return .= "<a class=&#39;now_page&#39;>$i</a>\n";

                }

                else

                {

                    if($this->now_page-$i>=4 && $i != 1)

                    {

                        $return .="<span class=&#39;pageMore&#39;>...</span>\n";

                        $i = $this->now_page-3;

                    }

                    else

                    {

                        if($i >= $this->now_page+5 && $i != $this->total_pages)

                        {

                            $return .="<span>...</span>\n";

                            $i = $this->total_pages;

                        }

                        $return .= $this->_get_link($i, $i) . "\n";

                    }

                }

            }

            $return .= $this->down_page(&#39;>&#39;);

            return $return;

        }

    }

      

    protected function show_1()

    {

        $plus = $this->plus;

        if( $plus + $this->now_page > $this->total_pages)

        {

            $begin = $this->total_pages - $plus * 2;

        }else{

            $begin = $this->now_page - $plus;

        }

          

        $begin = ($begin >= 1) ? $begin : 1;

        $return = &#39;&#39;;

        $return .= $this->first_page();

        $return .= $this->up_page();

        for ($i = $begin; $i <= $begin + $plus * 2;$i++)

        {

            if($i>$this->total_pages)

            {

                break;

            }

            if($i == $this->now_page)

            {

                $return .= "<a class=&#39;now_page&#39;>$i</a>\n";

            }

            else

            {

                $return .= $this->_get_link($i, $i) . "\n";

            }

        }

        $return .= $this->down_page();

        $return .= $this->last_page();

        return $return;

    }

      

    protected function show_3()

    {

        $plus = $this->plus;

        if( $plus + $this->now_page > $this->total_pages)

        {

            $begin = $this->total_pages - $plus * 2;

        }else{

            $begin = $this->now_page - $plus;

        }      

        $begin = ($begin >= 1) ? $begin : 1;

        $return = &#39;总计 &#39; .$this->total_rows. &#39; 个记录分为 &#39; .$this->total_pages. &#39; 页, 当前第 &#39; . $this->now_page . &#39; 页 &#39;;

        $return .= &#39;,每页 &#39;;

        $return .= &#39;<input type="text" value="&#39;.$this->list_rows.&#39;" id="pageSize" size="3"> &#39;;

        $return .= $this->first_page()."\n";

        $return .= $this->up_page()."\n";

        $return .= $this->down_page()."\n";

        $return .= $this->last_page()."\n";

        $return .= &#39;<select onchange="&#39;.$this->ajax_func_name.&#39;(this.value)" id="gotoPage">&#39;;

         

        for ($i = $begin;$i<=$begin+10;$i++)

        {

            if($i>$this->total_pages)

            {

                break;

            }          

            if($i == $this->now_page)

            {

                $return .= &#39;<option selected="true" value="&#39;.$i.&#39;">&#39;.$i.&#39;</option>&#39;;

            }

            else

            {

                $return .= &#39;<option value="&#39; .$i. &#39;">&#39; .$i. &#39;</option>&#39;;

            }          

        }

         $return .= &#39;</select>&#39;;

        return $return;

    }

}

Nach dem Login kopieren

使用方法

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

###处理html静态化页面分页的情况###

# method 处理环境 设置为 html

# parameter 为静态页面参数  xxx.com/20-0-0-0-40-?.html 注意问号

# ?问号的位置会自动替换为去向页码

# now_page 当前页面(静态页面获取不到当前页面所以只有你传入)

$params = array(

            &#39;total_rows&#39;=>100, #(必须)

            &#39;method&#39;    =>&#39;html&#39;, #(必须)

            &#39;parameter&#39; =>&#39;xxx.com/20-0-0-0-40-?.html&#39;,  #(必须)

            &#39;now_page&#39;  =>$_GET[&#39;p&#39;],  #(必须)

            &#39;list_rows&#39; =>10, #(可选) 默认为15

);

$page = new Core_Lib_Page($params);

echo  $page->show(1);

#<a href="xxx.com/20-0-0-0-40-2.html">2</a>

  

  

###处理ajax分页的情况###

# method 处理环境 设置为 ajax

# ajax_func_name ajax分页跳转页面的javascript方法

# parameter    ajax_func_name后面的附带参数 默认为空

# now_page 不到当前页面所以只有你传入

$params = array(

            &#39;total_rows&#39;=>100,

            &#39;method&#39;    =>&#39;ajax&#39;,

            &#39;ajax_func_name&#39; =>&#39;goToPage&#39;,

            &#39;now_page&#39;  =>1,

            #&#39;parameter&#39; =>"&#39;jiong&#39;,&#39;username&#39;",

);

$page = new Core_Lib_Page($params);

echo  $page->show(1);       

#<a href="javascript:void(0)" onclick="goToPage(&#39;7&#39;)">7</a>

#添加了parameter<a href="javascript:void(0)" onclick="goToPage(&#39;6&#39;,&#39;jiong&#39;,&#39;username&#39;)">6</a>

Nach dem Login kopieren
Verwandte Etiketten:
Quelle:php.cn
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
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage