Home >Backend Development >PHP Tutorial >How to implement paging function in webpy_PHP tutorial

How to implement paging function in webpy_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:58:23902browse

Paging is something you will definitely encounter when doing WEB development, but webpy does not have a built-in distribution class. You can only write a simple paging class by yourself. This article uses webpy (actually just a function of python) to implement a simple paging class...

How to paginate

In the past, paging and DB were often mixed together, such as the following PHP code:

<?php
$page = get_current_page();
$start = $page*$step;
$article_list = $db->all('select * from `xxx` limit $start,$step;');
$total = $db->get('select count(*) as `total` form `xxx`;');
//...pagination...

Such paging is obviously unscientific.

Things to note when paginating

  • Paging classes should not be confused with
  • Paging classes should not be confused with interfaces
  • Paging classes should be independent and can be used in any situation

In order to meet the above three conditions, a simple paging class was designed (just an example, application to the project requires optimized code and strict inspection)

Paging class

Paging class code

class ProbbsPage:
     def __init__(self, total, per = 10):
          self.total = total
          self.per = per
          self.url = ''
          self.page = 1
          
     def set_url(self,url):
          self.url = url
          return self
     
     def set_page(self,page):
          self.page = int(page)
          return self
          
     def show(self):
          if self.total%self.per == 0:
               pages = self.total/self.per
          else:
               pages = self.total/self.per+1
          if self.page < 6:
               limit_s = 1
          else:
               limit_s = self.page
          
          if pages < (limit_s+10):
               limit_e = pages
          else:
               limit_e = limit_s+10
          
          pagination = '<span>%s/%s pages </span>'%(self.page,pages)
          for i in range(limit_s,limit_e+1):
               if i == self.page:
                    pagination += '<a class="cur" href="javascript:void(0);">%s</a>'%(i,)
               else:
                    pagination += '<a href="%s">%s</a>'%(self.url%i,i)
          
          return pagination

Paging class call example

pagination =ProbbsPage(总页数,每页数)
url = 'your_page?page=%s';
page_html = pagination.set_url(url).set_page(page).show()
print page_html
#就可以显示出
#<span>当前页/共几页</span>
#<a>页页链接</a>

Where the category page can be optimized

set_url can be automatically extracted based on the url, provided that commonly used forms such as "?Page parameter=what page" are used (it is not possible if I use a special unexpected form)
set_page is the same as above, and can be automatically Extract from the url
If you do the above two steps, you can directly ProbbsPage (total number of pages, number of each page).show(), which is relatively convenient

Article source: http://pjiaxu.com/python/48.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/363817.htmlTechArticlePaging, you will definitely encounter it when doing WEB development, but webpy does not have a built-in distribution class, only Write a simple paging class by yourself. This article uses webpy (actually just py...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn