Introduction to pagination in Ecshop _ecshop

Source: Internet
Author: User

Paging in a large system is essential, when the amount of data is too large, the content of a page is limited, then we want to show the data page, below describes the next MySQL under the principle of pagination, as well as the use of ecshop in the paging function.


MySQL in the paging is mainly through the keyword limit to achieve, limit can give the starting value, and then give a range, you can take out all the data.
For a simple example:


CREATE table user (int not NULL auto_increment,
Name varchar (20),
Sex tinyint (1),
Reg_time datetime not NULL DEFAULT ' 0000-00-00 00:00:00 ',
) Engine=myisam auto_increment=1 DEFAULT Charset=utf8;


Create the user table and insert the test data.
Insert into user (' name ', ' Sex ' reg_time ') VALUES (' Zhangsan ', ' 1 ', ' 2012-02-09 02:00:00);
Insert into user (' name ', ' Sex ' reg_time ') VALUES (' Zaoren ', ' 1 ', ' 2012-02-09 14:00:32);
Insert into user (' name ', ' Sex ' reg_time ') VALUES (' Lily ', ' 0 ', ' 2013-08-10 10:06:00);
Insert into user (' name ', ' Sex ' reg_time ') VALUES (' Lizi ', ' 0 ', ' 1956-00-56 10:10:50);
Insert into user (' name ', ' Sex ' reg_time ') VALUES (' zhangsan2 ', ' 1 ', ' 1999-00-34 03:08:00);
Insert into user (' name ', ' Sex ' reg_time ') VALUES (' zhangsan3 ', ' 0 ', ' 1998-00-24 00:23:00);
Insert into user (' name ', ' Sex ' reg_time ') VALUES (' Zhangsan4 ', ' 1 ', ' 2013-00-00 00:12:00);
Insert into user (' name ', ' Sex ' reg_time ') VALUES (' Qunhao ', ' 1 ', ' 2014-00-00 00:00:00);
Insert into user (' name ', ' Sex ' reg_time ') VALUES (' Zhangsan4 ', ' 1 ', ' 2014-10-30 09:56:00);
Insert into user (' name ', ' Sex ' reg_time ') VALUES (' Zhangsan6 ', ' 1 ', ' 2013-06-00 05:01:00);
Insert into user (' name ', ' Sex ' reg_time ') VALUES (' Zhangsan8 ', ' 1 ', ' 2016-00-00 00:00:00);
Insert into user (' name ', ' Sex ' reg_time ') VALUES (' Hongyezhang ', ' 0 ', ' 2015-00-00 00:00:00);
Insert into user (' name ', ' Sex ' reg_time ') VALUES (' Zhangsan ', ' 1 ', ' 2008-08-080 00:00:00);


After that, the user information can be removed in batches via limit:
SELECT * from ' user ' order by ' reg_time ' limit 0, 5;
SELECT * from ' user ' order by ' reg_time ' limit 5, 5;
SELECT * from ' user ' order by ' reg_time ' limit 10, 5;
By changing the initial value of the limit, you can fetch 5 data at a time.


The page-number function in Ecshop is given below:


function Get_pager ($url, $param, $record _count, $page = 1, $size = 10)
{
$size = Intval ($size);
if ($size < 1)
{
$size = 10;
}


$page = Intval ($page);
if ($page < 1)
{
$page = 1;
}


$record _count = intval ($record _count);


$page _count = $record _count > 0? Intval (Ceil ($record _count/$size)): 1;
if ($page > $page _count)
{
$page = $page _count;
}
/* Pagination Style * *
$pager [' styleid '] = isset ($GLOBALS [' _cfg '] [' page_style '])? Intval ($GLOBALS [' _cfg '] [' Page_style ']): 0;


$page _prev = ($page > 1)? $page-1:1;
$page _next = ($page < $page _count)? $page + 1: $page _count;


/* to synthesize the parameter URL string * *
$param _url = '? ';
foreach ($param as $key => $value)
{
$param _url. = $key. '=' . $value. ' & ';
}


$pager [' url '] = $url;
$pager [' start '] = ($page-1) * $size;
$pager [' page '] = $page;
$pager [' size '] = $size;
$pager [' record_count '] = $record _count;
$pager [' page_count '] = $page _count;


if ($pager [' styleid '] = = 0)
{
$pager [' page_first '] = $url. $param _url. ' Page=1 ';
$pager [' page_prev '] = $url. $param _url. ' Page= '. $page _prev;
$pager [' page_next '] = $url. $param _url. ' Page= '. $page _next;
$pager [' page_last '] = $url. $param _url. ' Page= '. $page _count;
$pager [' array '] = Array ();
for ($i = 1; $i <= $page _count; $i + +)
{
$pager [' array '] [$i] = $i;
}
}
Else
{
$_pagenum = 10; Number of pages displayed
$_offset = 2; Current page Offset value
$_from = $_to = 0; Start Page, end page
if ($_pagenum > $page _count)
{
$_from = 1;
$_to = $page _count;
}
Else
{
$_from = $page-$_offset;
$_to = $_from + $_pagenum-1;
if ($_from < 1)
{
$_to = $page + 1-$_from;
$_from = 1;
if ($_to-$_from < $_pagenum)
{
$_to = $_pagenum;
}
}
ElseIf ($_to > $page _count)
{
$_from = $page _count-$_pagenum + 1;
$_to = $page _count;
}
}
$url _format = $url. $param _url. ' Page= ';
$pager [' page_first '] = ($page-$_offset > 1 && $_pagenum < $page _count)? $url _format. 1: ';
$pager [' page_prev '] = ($page > 1)? $url _format. $page _prev: ';
$pager [' page_next '] = ($page < $page _count)? $url _format. $page _next: ';
$pager [' page_last '] = ($_to < $page _count)? $url _format. $page _count: ';
$pager [' page_kbd '] = ($_pagenum < $page _count)? True:false;
$pager [' page_number '] = array ();
for ($i =$_from; $i <=$_to;++ $i)
{
$pager [' Page_number '] [$i] = $url _format. $i;
}
}
$pager [' search '] = $param;


return $pager;
}


From this we can see that he passed the value by falsifying the URL parameters, such as the argument page, where the current page is passed through the URL.


Pagination page numbering shows several variables that are required:


Current page variable,
The number of data bars,
The number of bars displayed per page,
Based on the total number of pages shown above,
This allows you to get the maximum number of page numbers, which can be displayed in pagination.


Given a url?page=2, the current is the second page, the second page number is not connected,
The number of the previous page is naturally url?page=1 (-1)
The page number of the next one is naturally url?page=3 (+1)


The contents of the current page, you can go through the


SELECT * FROM table order by ID limit 2*size, size,

All data in the fetch table can be reached each time by adjusting the limit initial value.


The implementation of pagination is to consider 2 parts, part of the data page display, part of the page number is displayed, the function given above is the ecshop of the page number display function, the data display through their own adjustment to achieve.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.