> 백엔드 개발 > PHP 튜토리얼 > PHP를 사용하여 구현된 2개의 페이징 클래스에 대한 자세한 설명

PHP를 사용하여 구현된 2개의 페이징 클래스에 대한 자세한 설명

PHPz
풀어 주다: 2021-05-31 13:45:21
앞으로
3035명이 탐색했습니다.

이 기사에서는 특히 사용하기 쉬운 두 가지 범용 PHP 페이징 클래스를 주로 소개합니다. 도움이 필요한 친구들이 모두 참고할 수 있기를 바랍니다.

PHP를 사용하여 구현된 2개의 페이징 클래스에 대한 자세한 설명

첫 번째 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

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

<?php

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

  

/**

 * 分页类

 * 使用方式:

 * $page = new Page();

 * $page->init(1000, 20);

 * $page->setNotActiveTemplate(&#39;<span> {a} </span>&#39;);

 * $page->setActiveTemplate(&#39;{a}&#39;);

 * echo $page->show();

 *

 *

 * @author 风居住的地方

 */

class Page {

  /**

   * 总条数

   */

  private $total;

  /**

   * 每页大小

   */

  private $pageSize;

  /**

   * 总页数

   */

  private $pageNum;

  /**

   * 当前页

   */

  private $page;

  /**

   * 地址

   */

  private $uri;

  /**

   * 分页变量

   */

  private $pageParam;

  /**

   * LIMIT XX,XX

   */

  private $limit;

  /**

   * 数字分页显示

   */

  private $listnum = 8;

  /**

   * 分页显示模板

   * 可用变量参数

   * {total}   总数据条数

   * {pagesize}  每页显示条数

   * {start}   本页开始条数

   * {end}    本页结束条数

   * {pagenum}  共有多少页

   * {frist}   首页

   * {pre}    上一页

   * {next}    下一页

   * {last}    尾页

   * {list}    数字分页

   * {goto}    跳转按钮

   */

  private $template = &#39;<div><span>共有{total}条数据</span><span>每页显示{pagesize}条数据</span>,<span>本页{start}-{end}条数据</span><span>共有{pagenum}页</span><ul>{frist}{pre}{list}{next}{last}{goto}</ul></div>&#39;;

  /**

   * 当前选中的分页链接模板

   */

  private $activeTemplate = &#39;<li class="active"><a href="javascript:;">{text}</a></li>&#39;;

  /**

   * 未选中的分页链接模板

   */

  private $notActiveTemplate = &#39;<li><a href="{url}">{text}</a></li>&#39;;

  /**

   * 显示文本设置

   */

  private $config = array(&#39;frist&#39; => &#39;首页&#39;, &#39;pre&#39; => &#39;上一页&#39;, &#39;next&#39; => &#39;下一页&#39;, &#39;last&#39; => &#39;尾页&#39;);

  /**

   * 初始化

   * @param type $total    总条数

   * @param type $pageSize  每页大小

   * @param type $param    url附加参数

   * @param type $pageParam  分页变量

   */

  public function init($total, $pageSize, $param = &#39;&#39;, $pageParam = &#39;page&#39;) {

    $this->total = intval($total);

    $this->pageSize = intval($pageSize);

    $this->pageParam = $pageParam;

    $this->uri = $this->geturi($param);

    $this->pageNum = ceil($this->total / $this->pageSize);

    $this->page = $this->setPage();

    $this->limit = $this->setlimit();

  }

    

  /**

   * 设置分页模板

   * @param type $template  模板配置

   */

  public function setTemplate($template) {

    $this->template = $template;

  }

    

  /**

   * 设置选中分页模板

   * @param type $activeTemplate   模板配置

   */

  public function setActiveTemplate($activeTemplate) {

    $this->activeTemplate = $activeTemplate;

  }

  

  /**

   * 设置未选中分页模板

   * @param type $notActiveTemplate  模板配置

   */

  public function setNotActiveTemplate($notActiveTemplate) {

    $this->notActiveTemplate = $notActiveTemplate;

  }

  

  /**

   * 返回分页

   * @return type

   */

  public function show() {

    return str_ireplace(array(

      &#39;{total}&#39;,

      &#39;{pagesize}&#39;,

      &#39;{start}&#39;,

      &#39;{end}&#39;,

      &#39;{pagenum}&#39;,

      &#39;{frist}&#39;,

      &#39;{pre}&#39;,

      &#39;{next}&#39;,

      &#39;{last}&#39;,

      &#39;{list}&#39;,

      &#39;{goto}&#39;,

    ), array(

      $this->total,

      $this->setPageSize(),

      $this->star(),

      $this->end(),

      $this->pageNum,

      $this->frist(),

      $this->prev(),

      $this->next(),

      $this->last(),

      $this->pagelist(),

      $this->gopage(),

    ), $this->template);

  }

    

  /**

   * 获取limit起始数

   * @return type

   */

  public function getOffset() {

    return ($this->page - 1) * $this->pageSize;

  }

    

  /**

   * 设置LIMIT

   * @return type

   */

  private function setlimit() {

    return "limit " . ($this->page - 1) * $this->pageSize . ",{$this->pageSize}";

  }

  

  /**

   * 获取limit

   * @param type $args

   * @return type

   */

  public function __get($args) {

    if ($args == "limit") {

      return $this->limit;

    } else {

      return null;

    }

  }

  

  /**

   * 初始化当前页

   * @return int

   */

  private function setPage() {

    if (!empty($_GET[$this->pageParam])) {

      if ($_GET[$this->pageParam] > 0) {

        if ($_GET[$this->pageParam] > $this->pageNum)

          return $this->pageNum;

        else

          return $_GET[$this->pageParam];

      }

    }

    return 1;

  }

  

  /**

   * 初始化url

   * @param type $param

   * @return string

   */

  private function geturi($param) {

    $url = $_SERVER[&#39;REQUEST_URI&#39;] . (strpos($_SERVER[&#39;REQUEST_URI&#39;], "?") ? "" : "?") . $param;

    $parse = parse_url($url);

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

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

      unset($params["page"]);

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

      return $url;

    } else {

      return $url;

    }

  }

  

  /**

   * 本页开始条数

   * @return int

   */

  private function star() {

    if ($this->total == 0) {

      return 0;

    } else {

      return ($this->page - 1) * $this->pageSize + 1;

    }

  }

  

  /**

   * 本页结束条数

   * @return type

   */

  private function end() {

    return min($this->page * $this->pageSize, $this->total);

  }

  

  /**

   * 设置当前页大小

   * @return type

   */

  private function setPageSize() {

    return $this->end() - $this->star() + 1;

  }

    

  /**

   * 首页

   * @return type

   */

  private function frist() {

    $html = &#39;&#39;;

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

      $html .= $this->replace("{$this->uri}&page=1", $this->config[&#39;frist&#39;], true);

    } else {

      $html .= $this->replace("{$this->uri}&page=1", $this->config[&#39;frist&#39;], false);

    }

    return $html;

  }

  

  /**

   * 上一页

   * @return type

   */

  private function prev() {

    $html = &#39;&#39;;

    if ($this->page > 1) {

      $html .= $this->replace($this->uri.&#39;&page=&#39;.($this->page - 1), $this->config[&#39;pre&#39;], false);

    } else {

      $html .= $this->replace($this->uri.&#39;&page=&#39;.($this->page - 1), $this->config[&#39;pre&#39;], true);

    }

    return $html;

  }

  

  /**

   * 分页数字列表

   * @return type

   */

  private function pagelist() {

    $linkpage = "";

    $lastlist = floor($this->listnum / 2);

    for ($i = $lastlist; $i >= 1; $i--) {

      $page = $this->page - $i;

      if ($page >= 1) {

        $linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false);

      } else {

        continue;

      }

    }

    $linkpage .= $this->replace("{$this->uri}&page={$this->page}", $this->page, true);

    for ($i = 1; $i <= $lastlist; $i++) {

      $page = $this->page + $i;

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

        $linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false);

      } else {

        break;

      }

    }

    return $linkpage;

  }

  

  /**

   * 下一页

   * @return type

   */

  private function next() {

    $html = &#39;&#39;;

    if ($this->page < $this->pageNum) {

      $html .= $this->replace($this->uri.&#39;&page=&#39;.($this->page + 1), $this->config[&#39;next&#39;], false);

    } else {

      $html .= $this->replace($this->uri.&#39;&page=&#39;.($this->page + 1), $this->config[&#39;next&#39;], true);

    }

    return $html;

  }

  

  /**

   * 最后一页

   * @return type

   */

  private function last() {

    $html = &#39;&#39;;

    if ($this->page == $this->pageNum) {

      $html .= $this->replace($this->uri.&#39;&page=&#39;.($this->pageNum), $this->config[&#39;last&#39;], true);

    } else {

      $html .= $this->replace($this->uri.&#39;&page=&#39;.($this->pageNum), $this->config[&#39;last&#39;], false);

    }

    return $html;

  }

  

  /**

   * 跳转按钮

   * @return string

   */

  private function gopage() {

    $html = &#39;&#39;;

    $html.=&#39; <input type="text" value="&#39; . $this->page . &#39;" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>&#39; . $this->pageNum . &#39;)?&#39; . $this->pageNum . &#39;:this.value;location=\&#39;&#39; . $this->uri . &#39;&page=\&#39;+page+\&#39;\&#39;}" style="width:25px;"/><input type="button" onclick="javascript:var page=(this.previousSibling.value>&#39; . $this->pageNum . &#39;)?&#39; . $this->pageNum . &#39;:this.previousSibling.value;location=\&#39;&#39; . $this->uri . &#39;&page=\&#39;+page+\&#39;\&#39;" value="GO"/>&#39;;

    return $html;

  }

  

  /**

   * 模板替换

   * @param type $replace   替换内容

   * @param type $result   条件

   * @return type

   */

  private function replace($url, $text, $result = true) {

    $template = ($result ? $this->activeTemplate : $this->notActiveTemplate);

      

     $html = str_replace(&#39;{url}&#39;, $url, $template);

     $html = str_replace(&#39;{text}&#39;, $text, $html);

    return $html;

  }

}

로그인 후 복사

두 번째 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

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

<?php

/*

 *本程序文件对分页程序进行了封装

 *

*/

 

class Page_Link

{

  var $page_max = 10; //一组页码的最大数

 

  var $page_num = 10; //总页数

  var $length = 20; //一页的数据条数

 

  var $isNextPage = true;

  var $isFirstPage = false;

 

  function Calculation_Page_Num( $total )

  {

    $this->page_num = ceil( $total / $this->length );

    return $this->page_num;

  }

 

  function Calculation_Min_Max( $act_page = 1 )

  {

    // 定义左右偏移量

    $py_left = 0;

    $py_right = 0;

    // 定义左右边界

    $bj_left = 0;

    $bj_right = 0;

    // 定义滚动区间边界

    $gd_left = 0;

    $gd_right = 0;

    // 判断是否需要分组

    if ( ( $this->page_num - $this->page_max ) <= 0 )

    {

      // 不需要分组

      $bj_left = 1;

      $bj_right = $this->page_num;

    }

    else

    {

      // 要进行分组

      // 判断容量的奇偶

      $tmp = $this->page_max % 2;

      if ( $tmp === 1 )

      {

        // 奇数

        $py_left = $py_right = ( $this->page_max - 1 ) / 2;

      }

      else

      {

        // 偶数

        $py_left = $this->page_max / 2 - 1;

        $py_right = $this->page_max / 2;

      }

      // 计算滚动区间

      $gd_left = 1 + $py_left;

      $gd_right = $this->page_num - $py_right;

      // 判断当前页是否落入了滚动区间

      if ( $act_page >= $gd_left && $act_page <= $gd_right )

      {

        // 区间内

        $bj_left = $act_page - $py_left;

        $bj_right = $act_page + $py_right;

      }

      else

      {

        // 区间外

        if ( ( $act_page - $py_left ) <= 1 )

        {

          // 左侧固定区间

          $bj_left = 1;

          $bj_right = $this->page_max;

        }

        else

        {

          $bj_left = $this->page_num - $this->page_max + 1;

          $bj_right = $this->page_num;

        }

      }

    }

 

    $res = array();

    $res[&#39;min&#39;] = $bj_left;

    $res[&#39;max&#39;] = $bj_right;

 

    return $res;

     

  }

  // 主方法

  function make_page( $total, $act_page, $url, $param )

  {

    $page_num = $this->Calculation_Page_Num( $total );

    $arr_min_max = $this->Calculation_Min_Max( $act_page );

     

    if (!eregi("([?|&]$param=)", $url)) {

      $url = strpos($url,"?")===false?$url."?":$url."&";

      $url = $url."$param=0";

    }

 

    if ( $act_page > $page_num )

    {

      $act_page = $page_num;

    }

    // 用正则把url改成正规的

    $url = eregi_replace( $param . &#39;=[0-9]+&#39;, $param . &#39;=0&#39;, $url );

 

    $res = array();

    $d = 0;

    for( $i = $arr_min_max[&#39;min&#39;];$i <= $arr_min_max[&#39;max&#39;];$i++ )

    {

      if ( $i == $act_page )

      {

        $res[$d][&#39;url&#39;] = &#39;&#39;;

        $res[$d][&#39;name&#39;] = $i;

        $res[$d][&#39;no&#39;] = $i;

      }

      else

      {

        $res[$d][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; . $i, $url );

        $res[$d][&#39;name&#39;] = $i;

        $res[$d][&#39;no&#39;] = $i;

      }

      $d++;

    }

 

    if ( $this->isNextPage )

    {

      $res = $this->make_before_next_link( $res, $act_page, $url, $param );

    }

    if ( $this->isFirstPage )

    {

      $res = $this->make_first_end_link( $res, $act_page, $url, $param );

    }

    return $res;

  }

  //// 带总页数

  function make_page_with_total( $total, $act_page, $url, $param )

  {

    $page_num = $this->Calculation_Page_Num( $total );

    $arr_min_max = $this->Calculation_Min_Max( $act_page );

     

    if (!eregi("([?|&]$param=)", $url)) {

      $url = strpos($url,"?")===false?$url."?":$url."&";

      $url = $url."$param=0";

    }

 

    if ( $act_page > $page_num )

    {

      $act_page = $page_num;

    }

    // 用正则把url改成正规的

    $url = eregi_replace( $param . &#39;=[0-9]+&#39;, $param . &#39;=0&#39;, $url );

 

    $res = array();

    $d = 0;

    for( $i = $arr_min_max[&#39;min&#39;];$i <= $arr_min_max[&#39;max&#39;];$i++ )

    {

      if ( $i == $act_page )

      {

        $res[$d][&#39;url&#39;] = &#39;&#39;;

        $res[$d][&#39;name&#39;] = $i;

        $res[$d][&#39;no&#39;] = $i;

      }

      else

      {

        $res[$d][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; . $i, $url );

        $res[$d][&#39;name&#39;] = $i;

        $res[$d][&#39;no&#39;] = $i;

      }

      $d++;

    }

 

    if ( $this->isNextPage )

    {

      $res = $this->make_before_next_link( $res, $act_page, $url, $param );

    }

    if ( $this->isFirstPage )

    {

      $res = $this->make_first_end_link( $res, $act_page, $url, $param );

    }

     

    $total_num= ceil($total/$this->length);

    $result[&#39;total&#39;]=$total_num;

    $result[&#39;DATA&#39;]=$res;

    return $result;

  }

   

  // 附加上一页和下一页

  function make_before_next_link( $arr, $act, $url, $param )

  {

    $tmp = array();

 

    $before = $act - 1;

    $next = $act + 1;

 

    if ( $before < 1 )

    {

      $before = 1;

      $tmp[0][&#39;url&#39;] = &#39;&#39;;

      $tmp[0][&#39;name&#39;] = "上一页";

      $tmp[0][&#39;no&#39;] = $before;

    }

    else

    {

      $tmp[0][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; . $before, $url );

      $tmp[0][&#39;name&#39;] = "上一页";

      $tmp[0][&#39;no&#39;] = $before;

    }

 

    $counts = sizeof( $arr );

    $tmp_count = sizeof( $tmp );

    for( $i = 0;$i < $counts;$i++ )

    {

      $tmp[$tmp_count][&#39;url&#39;] = $arr[$i][&#39;url&#39;];

      $tmp[$tmp_count][&#39;name&#39;] = $arr[$i][&#39;name&#39;];

      $tmp[$tmp_count][&#39;no&#39;] = $arr[$i][&#39;no&#39;];

      $tmp_count++;

    }

 

    if ( $next > $this->page_num )

    {

      $next = $this->page_num;

      $tmp[$tmp_count][&#39;url&#39;] = &#39;&#39;;

      $tmp[$tmp_count][&#39;name&#39;] = "下一页";

      $tmp[$tmp_count][&#39;no&#39;] = $next;

    }

    else

    {

      $tmp[$tmp_count][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; . $next, $url );

      $tmp[$tmp_count][&#39;name&#39;] = "下一页";

      $tmp[$tmp_count][&#39;no&#39;] = $next;

    }

 

    return $tmp;

  }

   

  // 附加首页和尾页

  function make_first_end_link( $arr, $act, $url, $param )

  {

    $tmp = array();

 

    $before = 1;

    $next = $this->page_num;

 

    if ( $act == 1 )

    {

      $before = 1;

      $tmp[0][&#39;url&#39;] = &#39;&#39;;

      $tmp[0][&#39;name&#39;] = "首页";

      $tmp[0][&#39;no&#39;] = $before;

    }

    else

    {

      $tmp[0][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; . $before, $url );

      $tmp[0][&#39;name&#39;] = "首页";

      $tmp[0][&#39;no&#39;] = $before;

    }

 

    $counts = sizeof( $arr );

    $tmp_count = sizeof( $tmp );

    for( $i = 0;$i < $counts;$i++ )

    {

      $tmp[$tmp_count][&#39;url&#39;] = $arr[$i][&#39;url&#39;];

      $tmp[$tmp_count][&#39;name&#39;] = $arr[$i][&#39;name&#39;];

      $tmp[$tmp_count][&#39;no&#39;] = $arr[$i][&#39;no&#39;];

      $tmp_count++;

    }

 

    if ( $act == $this->page_num )

    {

      $tmp[$tmp_count][&#39;url&#39;] = &#39;&#39;;

      $tmp[$tmp_count][&#39;name&#39;] = "尾页";

      $tmp[$tmp_count][&#39;no&#39;] = $next;

    }

    else

    {

      $tmp[$tmp_count][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; . $next, $url );

      $tmp[$tmp_count][&#39;name&#39;] = "尾页";

      $tmp[$tmp_count][&#39;no&#39;] = $next;

    }

 

    return $tmp;

  }

   

    

  /**

   * 带上一页<,下一页>,省略号的分页

   * @param int $total    记录总条数

   * @param int $act_page    当前页码

   * @param string $url    url

   * @param int $maxpageicon  最大显示页码数

   * @param int $style    上一页,下一页显示样式

   * @param string $param    url参数

   */

  function make_page_with_points( $total,$act_page,$url,$maxpageicon,$style,$param )

  {

    $page_num = $this->Calculation_Page_Num( $total );    //总页数

    $arr_min_max = $this->Calculation_Min_Max( $act_page );    //最大页,最小页 

    if($total==0)

    {

       return "";

 

    }

    if( $act_page > $page_num )

    {

      $act_page = $page_num+1;

      $page_num = $page_num+1;

    }

     

    switch ($style){

      case 1:

        $name_before = &#39;前一页&#39;;

        $name_next = &#39;后一页&#39;;

        break;

      case 2:

        $name_before = &#39;<&#39;;

        $name_next = &#39;>&#39;;

        break;

      case 3:

        $name_before = &#39;<<&#39;;

        $name_next = &#39;>>&#39;;

        break;

      default:

        $name_before = &#39;上一页&#39;;

        $name_next = &#39;下一页&#39;;

    }

     

    if (!eregi("([?|&]$param=)", $url)) {

      $url = strpos($url,"?")===false?$url."?":$url."&";

      $url = $url."$param=0";

    }

         

    // 用正则把url改成正规的

    $url = eregi_replace( $param . &#39;=[0-9]+&#39;, $param . &#39;=0&#39;, $url );

    $res = array();

    $no_before = $act_page-1;

    $no_next = $act_page+1;

     

    //总页数如果小于等于初始化最大呈现页数

    if ($page_num<= ($maxpageicon + 1))

    {

      //如果当前页数是首页 上一页无效

      if ($act_page == 1) 

      {

        $res[0][&#39;url&#39;] = &#39;&#39;;

        $res[0][&#39;name&#39;] = $name_before;

        $res[0][&#39;no&#39;] = $no_before;

      }

      else      //上一页有效

      {

        $res[0][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; .($act_page - 1), $url );

        $res[0][&#39;name&#39;] = $name_before;

        $res[0][&#39;no&#39;] = $no_before;

      }

      //循环添加页码

      $d = 1;

      for ($i = 1; $i <= $page_num; $i++)

      {

        if ($i != $act_page)

        {

          $res[$d][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; . $i, $url );

          $res[$d][&#39;name&#39;] = $i;

          $res[$d][&#39;no&#39;] = $i;

        }

        else  //当前页,页码

        {

          $res[$d][&#39;url&#39;] = &#39;&#39;;

          $res[$d][&#39;name&#39;] = $i;

          $res[$d][&#39;no&#39;] = $i;

          $res[$d][&#39;attr&#39;] = &#39;current&#39;;

        }

        $d++;

      }

      $last_d = count($res);

      //判断尾页

      if($act_page == $page_num//下一页无效

      {

        $res[$last_d][&#39;url&#39;] = &#39;&#39;;

        $res[$last_d][&#39;name&#39;] = $name_next;

        $res[$last_d][&#39;no&#39;] = $no_next;   

      }

      else

      {

         $res[$last_d][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; .($act_page + 1), $url );

        $res[$last_d][&#39;name&#39;] = $name_next;

        $res[$last_d][&#39;no&#39;] = $no_next;

      }

    }else if ($page_num > ($maxpageicon + 1))//如果总页数满足添加省略号

    {

      if ($act_page <= $maxpageicon) //如果当前页小于等于初始化数目

      {

        //如果当前页数是首页 上一页无效

        if ($act_page == 1) 

        {

          $res[0][&#39;url&#39;] = &#39;&#39;;

          $res[0][&#39;name&#39;] = $name_before;

          $res[0][&#39;no&#39;] = $no_before;

        }

        else      //上一页有效

        {

          $res[0][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; .($act_page - 1), $url );

          $res[0][&#39;name&#39;] = $name_before;

          $res[0][&#39;no&#39;] = $no_before;

        }

        //循环添加页码

        $d = 1;

        for ($i = 1; $i <= $maxpageicon; $i++)

        {

          if ($i != $act_page)

          {

            $res[$d][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; . $i, $url );

            $res[$d][&#39;name&#39;] = $i;

            $res[$d][&#39;no&#39;] = $i;

          }

          else  //当前页,页码

          {

            $res[$d][&#39;url&#39;] = &#39;&#39;;

            $res[$d][&#39;name&#39;] = $i;

            $res[$d][&#39;no&#39;] = $i;

            $res[$d][&#39;attr&#39;] = &#39;current&#39;;

          }

          $d++;

        }

        $last_d = count($res);

        //添加省略号

        $res[$last_d][&#39;url&#39;] = &#39;&#39;;

        $res[$last_d][&#39;name&#39;] = &#39;...&#39;;

        $res[$last_d][&#39;no&#39;] = &#39;&#39;;

        //总页数

        $res[$last_d+1][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; . $page_num, $url );

        $res[$last_d+1][&#39;name&#39;] = $page_num;

        $res[$last_d+1][&#39;no&#39;] = $page_num;

        //下一页

        $res[$last_d+1][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; . ($act_page + 1), $url );

        $res[$last_d+1][&#39;name&#39;] = $name_next;

        $res[$last_d+1][&#39;no&#39;] = $no_next;    

      }else//如果当前页大于最大显示页面

      {

        if ($act_page > ($page_num - $maxpageicon))//满足后几页

        {

          //上一页

          $res[0][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; .($act_page - 1), $url );

          $res[0][&#39;name&#39;] = $name_before;

          $res[0][&#39;no&#39;] = $no_before;

          //第一页

          $res[1][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=1&#39;, $url );

          $res[1][&#39;name&#39;] = 1;

          $res[1][&#39;no&#39;] = 1; 

          //省略号

          $res[2][&#39;url&#39;] = &#39;&#39;;

          $res[2][&#39;name&#39;] = &#39;...&#39;;

          $res[2][&#39;no&#39;] = &#39;&#39;;

          $d = 3;

          for ($i = ($page_num - $maxpageicon + 1); $i <= $page_num; $i++)

          {

            if ($i != $act_page)

            {

              $res[$d][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; . $i, $url );

              $res[$d][&#39;name&#39;] = $i;

              $res[$d][&#39;no&#39;] = $i;

            }

            else  //当前页,页码

            {

              $res[$d][&#39;url&#39;] = &#39;&#39;;

              $res[$d][&#39;name&#39;] = $i;

              $res[$d][&#39;no&#39;] = $i;

              $res[$d][&#39;attr&#39;] = &#39;current&#39;;

            }

            $d++;

          }

          $last_d = count($res);

          //判断尾页

          if($act_page == $page_num//下一页无效

          {

             $res[$last_d][&#39;url&#39;] = &#39;&#39;;

            $res[$last_d][&#39;name&#39;] = $name_next;

            $res[$last_d][&#39;no&#39;] = $no_next;   

          }

          else

          {

             $res[$last_d][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; .($act_page + 1), $url );

            $res[$last_d][&#39;name&#39;] = $name_next;

            $res[$last_d][&#39;no&#39;] = $no_next;

          }

 

        }else//满足处在中间

        {

          //上一页

          $res[0][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; .($act_page - 1), $url );

          $res[0][&#39;name&#39;] = $name_before;

          $res[0][&#39;no&#39;] = $no_before;

          //第一页

          $res[1][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=1&#39;, $url );

          $res[1][&#39;name&#39;] = 1;

          $res[1][&#39;no&#39;] = 1; 

          //省略号

          $res[2][&#39;url&#39;] = &#39;&#39;;

          $res[2][&#39;name&#39;] = &#39;...&#39;;

          $res[2][&#39;no&#39;] = &#39;&#39;;

          for ($i = ($act_page - ($maxpageicon - 2) / 2); $i <= floor($act_page+($maxpageicon - 2) / 2); $i++)

          {

            $i = ceil($i);

            if ($i != $act_page)

            {

              $res[$d][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; . $i, $url );

              $res[$d][&#39;name&#39;] = $i;

              $res[$d][&#39;no&#39;] = $i;

            }

            else  //当前页,页码

            {

              $res[$d][&#39;url&#39;] = &#39;&#39;;

              $res[$d][&#39;name&#39;] = $i;

              $res[$d][&#39;no&#39;] = $i;

              $res[$d][&#39;attr&#39;] = &#39;current&#39;;

            }

            $d++;

          }

          $last_d = count($res);

          //加省略号

          $res[$last_d][&#39;url&#39;] = &#39;&#39;;

          $res[$last_d][&#39;name&#39;] = &#39;...&#39;;

          $res[$last_d][&#39;no&#39;] = &#39;&#39;;

          //当前页

          $res[$last_d+1][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; . $page_num, $url );

          $res[$last_d+1][&#39;name&#39;] = $page_num;

          $res[$last_d+1][&#39;no&#39;] = $page_num;   

          //下一页

          $res[$last_d+2][&#39;url&#39;] = str_replace( $param . &#39;=0&#39;, $param . &#39;=&#39; . ($act_page + 1), $url );

          $res[$last_d+2][&#39;name&#39;] = $name_next;

          $res[$last_d+2][&#39;no&#39;] = $no_next;

          //exit; 

         }

      }

    }

    return $res;

  }

}

 

?>

로그인 후 복사

추천 학습: "PHP 비디오 튜토리얼"

관련 라벨:
php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿