列表分页类,jQuery制作的列表分页的作用模块

1 /*  2 ******* 环境:Apache2.2.8 ( 2.2.17 ) + PHP5.2.6 ( 5.3.3 ) + MySQL5.0.51b ( 5.5.8 ) + jQuery-1.8.3.min.js + Smarty 3.1.18 *******  3 ******* Date:2014-10-11  4 ******* Author:小dee  5 ******* Blog:http://www.cnblogs.com/dee0912/  6 */

做了叁个列表分页的效能模块,首要的文本富含分页类 page.class.php 和 控制ajax 分页的ajax.js,首要作用有:

此前本身直接用Aspnetpager控件来呈现档案的次序中的分页,不过每回都要拖二个aspnetpager的控件进去,认为很不爽直,因为未来温馨写的webform都毫无服务器控件了,所以本人模仿aspnetpager写了两个简短实用的回到分页彰显的html方法,别的话不说了,间接上代码。

前边自身一向用Aspnetpager控件来展现档期的顺序中的分页,然而每趟都要拖三个aspnetpager的控件进去,以为很不舒服,因为未来和好写的webform都休想服务器控件了,所以本人模仿aspnetpager写了八个简洁实用的归来分页展现的html方法,其余话不说了,直接上代码。
 
分页展现消息的实体类:
  View Code
public class Pager
    {
        private string _firstPageText;
        /// <summary>
        /// 最前一页文字展现 暗中认可展现为”首页”
        /// </summary>
        public string FirstPageText
        {
            get {
                return string.IsNullOrEmpty(_firstPageText)? “首页” :
_firstPageText;
            }
            set {
                _firstPageText = value;
            }
        }

做了多个列表分页的功用模块,首要的文件饱含分页类 page.class.php 和 控制ajax 分页的ajax.js,重要成效有:

1.方可挑选 3 种广泛的 url 分页格式;

分页展现新闻的实体类:
View Code
public class Pager
{
private string _firstPageText;
/// <summary>
/// 最前一页文字呈现 暗许突显为”首页”
/// </summary>
public string FirstPageText
{
get {
return string.IsNullOrEmpty(_firstPageText)? “首页” :
_firstPageText;
}
set {
_firstPageText = value;
}
}

        private string _prePageText;
        /// <summary>
        /// 上一页文字彰显 暗中认可展现为”上一页”
        /// </summary>
        public string PrePageText
        {
            get
            {
                return string.IsNullOrEmpty(_prePageText) ? “上一页” :
_prePageText;
            }
            set
            {
                _prePageText = value;
            }
        }

1.方可选用 3 种普及的 url 分页格式;

2.能够选用 url 分页 依旧 ajax 分页;

private string _prePageText;
/// <summary>
/// 上一页文字显示 暗中认可显示为”上一页”
/// </summary>
public string PrePageText
{
get
{
return string.IsNullOrEmpty(_prePageText) ? “上一页” : _prePageText;
}
set
{
_prePageText = value;
}
}

        private string _列表分页类,jQuery制作的列表分页的作用模块。nextPageText;
        /// <summary>
        /// 下一页文字展现 暗许展现为”下一页”
        /// </summary>
        public string NextPageText
        {
            get
            {
                return string.IsNullOrEmpty(_nextPageText) ? “下一页” :
_nextPageText;
            }
            set
            {
                _nextPageText = value;
            }
        }

2.得以挑选 url 分页 照旧 ajax 分页;

3.二种分页格局都足以自定义分页 a 标签的文字;

private string _nextPageText;
/// <summary>
/// 下一页文字呈现 暗许显示为”下一页”
/// </summary>
public string NextPageText
{
get
{
return string.IsNullOrEmpty(_nextPageText) ? “下一页” :
_nextPageText;
}
set
{
_nextPageText = value;
}
}

        private string _lastPageText;
        /// <summary>
        /// 末页文字显示 暗许呈现为”末页”
        /// </summary>
        public string LastPageText
        {
            get
            {
                return string.IsNullOrEmpty(_lastPageText) ? “末页” :
_lastPageText;
            }
            set
            {
                _lastPageText = value;
            }
        }

3.两种分页方式都得以自定义分页 a 标签的文字;

4.url 分页格局得以自定义分页偏移量;

private string _lastPageText;
/// <summary>
/// 末页文字显示 默许呈现为”末页”
/// </summary>
public string LastPageText
{
get
{
return string.IsNullOrEmpty(_lastPageText) ? “末页” : _lastPageText;
}
set
{
_lastPageText = value;
}
}

        /// <summary>
        /// 总记录数
        /// </summary>
        public int RecordCount { get; set; }

4.url 分页格局得以自定义分页偏移量;

5.url 分页情势能够选拔手动跳转情势:手动输入页码跳转 或
下拉菜单选用页码跳转。

/// <summary>
/// 总记录数
/// </summary>
public int RecordCount { get; set; }

        private int _pagesize=15;
        /// <summary>
        /// 每页分页尺寸 默感觉15
        /// </summary>
        public int PageSize {
            get {
                return _pagesize == 0 ? 15 : _pagesize;
            }set{
                _pagesize = value;
            }
        }

5.url 分页格局得以选拔手动跳转格局:手动输入页码跳转 或
下拉菜单采取页码跳转。

列表分页功效含有但不肯定全部分包的因素富含:

private int _pagesize=15;
/// <summary>
/// 每页分页尺寸 默认为15
/// </summary>
public int PageSize {
get {
return _pagesize == 0 ? 15 : _pagesize;
}set{
_pagesize = value;
}
}

        private int _pageIndex=1;
        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageIndex {
            get {
                return _pageIndex == 0 ? 1 : _pageIndex;
            }
            set {
                _pageIndex = value;
            }
        }

 

首页、下一页、上一页、末页、具体页码、手动输入的跳转至第几页、下拉菜单选取跳转至第几页、音信(
共多少页、共多少条、当前是第几页 )等。

private int _pageIndex=1;
/// <summary>
/// 当前页码
/// </summary>
public int PageIndex {
get {
return _pageIndex == 0 ? 1 : _pageIndex;
}
set {
_pageIndex = value;
}
}

        private int _maxShowPageSize = 10;
        /// <summary>
        /// 展现页码列表的最大个数 默以为10
        /// </summary>
        public int MaxShowPageSize {
            get {
                return _maxShowPageSize;
            }
            set {
                _maxShowPageSize = value;
            }
        }

列表分页成效含有但不必然全体富含的要素包含:

个中必须带有的要素有:上一页、下一页、具体页码。

private int _maxShowPageSize = 10;
/// <summary>
/// 显示页码列表的最大个数 默感觉10
/// </summary>
public int MaxShowPageSize {
get {
return _maxShowPageSize;
}
set {
_maxShowPageSize = value;
}
}

        private string _queryStringName;
        /// <summary>
        /// 页码在浏览器中传值的名目  默以为page
        /// </summary>
        public string QueryStringName {
            get {
                return string.IsNullOrEmpty(_queryStringName)? “page” :
_queryStringName;
            }
            set {
                _queryStringName = value;
            }
        }

首页、下一页、上一页、末页、具体页码、手动输入的跳转至第几页、下拉菜单选用跳转至第几页、新闻(
共多少页、共多少条、当前是第几页 )等。

先看看其余网址是如何做的( 百度搜索、果壳网、天猫商城、虾米、织梦官方网站 ):

private string _queryStringName;
/// <summary>
/// 页码在浏览器中传值的称谓 默以为page
/// </summary>
public string QueryStringName {
get {
return string.IsNullOrEmpty(_queryStringName)? “page” :
_queryStringName;
}
set {
_queryStringName = value;
}
}

        /// <summary>
        /// 页面的URL
        /// </summary>
        public string URL {
            get {
                string url =
HttpContext.Current.Request.Url.AbsoluteUri;//当前页面相对路径
                if (EnableUrlRewriting)//使用url重写
                {
                    url=url.Substring(0, url.LastIndexOf(“/”) +
1);//获取当前页面包车型客车目录路线
                    url += UrlRewritePattern;
                }
                else {//普通带问号的页面传值
                    //demo.aspx
                    //demo.aspx?a=1
                    //demo.aspx?page=1
                    //demo.aspx?a=2&page=1
                    if (url.Contains(“aspx?”))
                    {
                        if
(Regex.IsMatch(url,@”page=[0-9]*$”,RegexOptions.IgnoreCase))//若是存在page=*的字符串
                        {
                            url = Regex.Replace(url, @”page=[0-9]*$”,
“”, RegexOptions.IgnoreCase);//替换掉page=*的字符串
                        }
                        url += QueryStringName + “={0}”;
                    }
                    else {
                        url += “?” + QueryStringName + “={0}”;
                    }
                }
                return url;
            }
        }

其间必须含有的成分有:上一页、下一页、具体页码。

1.百度招来便是由最简便易行的”上一页”、”下一页”和实际页码构成。分页偏移量为前5页后4页

/// <summary>
/// 页面的URL
/// </summary>
public string URL {
get {
string url =
HttpContext.Current.Request.Url.AbsoluteUri;//当前页面相对路线
if (EnableUrlRewriting)//使用url重写
{
url=url.Substring(0, url.LastIndexOf;//获取当前页面的目录路线
url += UrlRewritePattern;
}
else {//普通带问号的页面传值
//demo.aspx
//demo.aspx?a=1
//demo.aspx?page=1
//demo.aspx?a=2&page=1
if (url.Contains
{
if
(Regex.IsMatch(url,@”page=[0-9]*$”,RegexOptions.IgnoreCase))//假设存在page=*的字符串
{
url = Regex.Replace(url, @”page=[0-9]*$”, “”,
RegexOptions.IgnoreCase);//替换掉page=*的字符串
}
url += QueryStringName + “={0}”;
}
else {
url += “?” + QueryStringName + “={0}”;
}
}
return url;
}
}

        private bool _enableUrlRewriting;
        /// <summary>
        /// UPanameraL是或不是重写 默以为flase
        /// </summary>
        public bool EnableUrlRewriting {
            get {
                return (object)_enableUrlRewriting == null ? false :
_enableUrlRewriting;
            }
            set {
                _enableUrlRewriting = value;
            }
        }

先看看其余网站是怎么办的( 百度寻找、网易、天猫商城、虾米、织梦官方网址 ):

美高梅开户网址 1

private bool _enableUrlRewriting;
/// <summary>
/// U福特ExplorerL是或不是重写 默以为flase
/// </summary>
public bool EnableUrlRewriting {
get {
return _enableUrlRewriting == null ? false : _enableUrlRewriting;
}
set {
_enableUrlRewriting = value;
}
}

        /// <summary>
        /// 页面U安德拉L重写法规,将页码传值用{0}来庖代 
举例list-1-{0}.html,启用该意义须求将EnableUrlRewriting属性设为true
        /// </summary>
        public string UrlRewritePattern { get; set; }

1.百度查找就是由最简易的”上一页”、”下一页”和具体页码构成。分页偏移量为前5页后4页

美高梅开户网址 2

/// <summary>
/// 页面U君越L重写准绳,将页码传值用{0}来取代比方list-1-{0}.html,启用该功效必要将EnableUrlRewriting属性设为true
/// </summary>
public string UrlRewritePattern { get; set; }

        private string _className;
        /// <summary>
        /// 分页容器的css名称
        /// </summary>
        public string ClassName {
            get {
                return string.IsNullOrEmpty(_className) ? “paginator” :
_className;
            }set{
                _className = value;
            }
        }

美高梅开户网址 3

美高梅开户网址 4

private string _className;
/// <summary>
/// 分页容器的css名称
/// </summary>
public string ClassName {
get {
return string.IsNullOrEmpty(_className) ? “paginator” : _className;
}set{
_className = value;
}
}

        private string _currentPageCss;
        /// <summary>
        /// 当前页面按键css
        /// </summary>
        public string CurrentPageButtonCss {
            get {
                return string.IsNullOrEmpty(_currentPageCss) ? “cpb” :
_currentPageCss;
            }set{
                _currentPageCss = value;
            }
        }

美高梅开户网址 5

2.天涯论坛话题(

) 是同样的管理格局。

private string _currentPageCss;
/// <summary>
/// 当前页面开关css
/// </summary>
public string CurrentPageButtonCss {
get {
return string.IsNullOrEmpty(_currentPageCss) ? “cpb” :
_currentPageCss;
}set{
_currentPageCss = value;
}
}

        private bool _showSpanText;
        /// <summary>
        /// Span 标签中文字消息是不是出示 默感觉false不呈现
        /// </summary>
        public bool ShowSpanText {
            get {
                return (object)_showSpanText == null ? false :
_showSpanText;
            }
            set {
                _showSpanText = value;
            }
        }

 美高梅开户网址 6

美高梅开户网址 7

private bool _showSpanText;
/// <summary>
/// Span 标签普通话字音讯是或不是出示 默感觉false不突显
/// </summary>
public bool ShowSpanText {
get {
return _showSpanText == null ? false : _showSpanText;
}
set {
_showSpanText = value;
}
}

        private string _spanTextClass;
        /// <summary>
        /// 分页文字描述span标签css
        /// </summary>
        public string SpanTextClass {
            get {
                return string.IsNullOrEmpty(_spanTextClass) ? “stc” :
_spanTextClass;
            }
            set {
                _spanTextClass = value;
            }
        }

 

美高梅开户网址 8

private string _spanTextClass;
/// <summary>
/// 分页文字描述span标签css
/// </summary>
public string SpanTextClass {
get {
return string.IsNullOrEmpty(_spanTextClass) ? “stc” :
_spanTextClass;
}
set {
_spanTextClass = value;
}
}

        private string _submitButtonText;
        /// <summary>
        /// 鲜明按键文字突显 私下认可突显”分明”
        /// </summary>
        public string SubmitButtonText {
            get {
                return string.IsNullOrEmpty(_submitButtonText) ? “确定”
: _submitButtonText;
            }
            set {
                _submitButtonText = value;
            }
        }
    }

2.知乎话题(
 )的页码蕴含了”上一页”、”下一页”、具体页码、手动输入跳转至第几页、新闻等因素,还包蕴首页和末页,只可是这里的首页和末页不是用文字而是用实际页码表现出来。分页偏移量前后都以4页。今日头条的列表页(
 ) 是大同小异的处理格局。

美高梅开户网址 9

private string _submitButtonText;
/// <summary>
/// 鲜明开关文字显示 默许展现”分明”
/// </summary>
public string SubmitButtonText {
get {
return string.IsNullOrEmpty(_submitButtonText) ? “确定” :
_submitButtonText;
}
set {
_submitButtonText = value;
}
}
}

 
分页显示html代码的拼凑方法:
  View Code
public class SplitManager
    {
                public static string AspNetPagers(Pager pager)
        {
            StringBuilder sb = new StringBuilder();
            string attr=””;
            int pagecount = 0;//当前页面包车型客车总层数
            int floorcount = 0;//分页的总层数
            int currentLastPage = 0;//当前最后一页的页码
            int pageNum = pager.RecordCount / pager.PageSize +
1;//总页数 1~24
            sb.AppendFormat(“<div class=\”{0}\”>\n”,
pager.ClassName);
           
attr=pager.PageIndex==1?”disabled=disabled”:””;//标记当前页第一页是或不是等于
来调控前俩个按键的有效
           
sb.AppendFormat(GetAHtml(attr,string.Format(pager.UENCOREL,1),pager.FirstPageText));//增添最前一页的代码
            sb.AppendFormat(GetAHtml(attr, string.Format(pager.U福特ExplorerL,
pager.PageIndex-1), pager.PrePageText));//添加上一页的代码
            pagecount = pager.PageIndex/pager.马克斯ShowPageSize;//当前页数
0~1~2
            pagecount = pager.PageIndex % pager.马克斯ShowPageSize == 0 ?
pagecount – 1 : pagecount;//清除当
当前页数为分页页码数的莫西干发型倍页时除数多一的风貌
            floorcount = pageNum / pager.MaxShowPageSize;//页面层数
0~1~2
            currentLastPage=pageNum<pager.MaxShowPageSize *
(pagecount + 1)?pageNum:pager.MaxShowPageSize * (pagecount + 1);
            if (pager.PageIndex >
pager.马克斯ShowPageSize)//当当前序号大于每页页码个数时显示再前端…
            {
                sb.AppendFormat(GetAHtml(“”, string.Format(pager.URL,
pager.MaxShowPageSize * pagecount), “…”));
            }
            for (int i = pager.MaxShowPageSize * pagecount + 1; i <=
currentLastPage; i++)
            {
                if (i == pager.PageIndex)//推断循环页面是或不是为当前页
                {
                    sb.AppendFormat(GetSpanHtml(i,
pager.CurrentPageButtonCss));
                }
                else {
                    sb.AppendFormat(GetAHtml(“”,
string.Format(pager.URL, i),i.ToString()));
                }
            }
            if (pager.PageIndex <= pager.MaxShowPageSize *
floorcount)//当当前序号小于尾数第二页页码时彰显在后端…
            {
                sb.AppendFormat(GetAHtml(“”, string.Format(pager.URL,
pager.MaxShowPageSize * (pagecount + 1) + 1), “…”));
            }
            attr = pager.PageIndex == pageNum ? “disabled=disabled” :
“”;//标记当前页最终一页是或不是等于 来调整后俩个按键的立竿见影
            sb.AppendFormat(GetAHtml(attr, string.Format(pager.UOdysseyL,
pager.PageIndex+1), pager.NextPageText));//增加后一页的代码
            sb.AppendFormat(GetAHtml(attr, string.Format(pager.UOdysseyL,
pageNum), pager.LastPageText));//增加最后一页的代码
            if (pager.ShowSpanText)//是或不是出示分页文字span标签呈现
            {
                sb.AppendFormat(“<span class=\”” +
pager.SpanTextClass + “\”>共{0}页,每页{1}条记录 \n”, pageNum,
pager.PageSize);
                sb.AppendFormat(“到第<input type=\”input\”
id=\”jumpNum\” style=\”width:20px;\” name=\”jump\” value=\”{0}\”
/>页”, pager.PageIndex == pageNum ? pageNum : pager.PageIndex + 1);
                sb.AppendFormat(“<a href=\”#\”
style=\”float:none;\” onclick=\”javascript:jump();\”>” +
pager.SubmitButtonText + “</a></span>\n”);
               
sb.Append(GetJumpScript(pager.USportageL));//增多开关跳转的javascript代码
            }
            sb.AppendFormat(“</div>”);//
            return sb.ToString();
        }

美高梅开户网址 10

美高梅开户网址 11

分页显示html代码的拼凑方法:
View Code
public class SplitManager
{
public static string AspNetPagers(Pager pager)
{
StringBuilder sb = new StringBuilder();
string attr=””;
int pagecount = 0;//当前页面包车型大巴总层数
int floorcount = 0;//分页的总层数
int currentLastPage = 0;//当前最后一页的页码
int pageNum = pager.RecordCount / pager.PageSize + 1;//总页数 1~24
sb.AppendFormat(“<div class=\”{0}\”>\n”, pager.ClassName);
attr=pager.PageIndex==1?”disabled=disabled”:””;//标识当前页第一页是不是等于
来调节前俩个开关的管事
sb.AppendFormat(GetAHtml(attr,string.Format(pager.ULacrosseL,1),pager.FirstPageText));//增添最前一页的代码
sb.AppendFormat(GetAHtml(attr, string.Format(pager.U汉兰达L,
pager.PageIndex-1), pager.PrePageText));//增多上一页的代码
pagecount = pager.PageIndex/pager.马克斯ShowPageSize;//当前页数 0~1~2
pagecount = pager.PageIndex % pager.马克斯ShowPageSize == 0 ? pagecount – 1
: pagecount;//清除当 当前页数为分页页码数的卡尺头倍页时除数多一的场景
floorcount = pageNum / pager.马克斯ShowPageSize;//页面层数 0~1~2
currentLastPage=pageNum<pager.MaxShowPageSize * (pagecount +
1)?pageNum:pager.MaxShowPageSize * (pagecount + 1);
if (pager.PageIndex >
pager.马克斯ShowPageSize)//当当前序号大于每页页码个数时显得再前端…
{
sb.AppendFormat(GetAHtml(“”, string.Format(pager.URL,
pager.MaxShowPageSize * pagecount), “…”));
}
for (int i = pager.MaxShowPageSize * pagecount + 1; i <=
currentLastPage; i++)
{
if (i == pager.PageIndex)//判别循环页面是不是为当前页
{
sb.AppendFormat(GetSpanHtml(i, pager.CurrentPageButtonCss));
}
else {
sb.AppendFormat(GetAHtml(“”, string.Format(pager.URL, i),i.ToString;
}
}
if (pager.PageIndex <= pager.MaxShowPageSize *
floorcount)//当当前序号小于尾数第二页页码时突显在后端…
{
sb.AppendFormat(GetAHtml(“”, string.Format(pager.URL,
pager.MaxShowPageSize * (pagecount + 1) + 1), “…”));
}
attr = pager.PageIndex == pageNum ? “disabled=disabled” :
“”;//标识当前页最终一页是还是不是等于 来调控后俩个按键的实用
sb.AppendFormat(GetAHtml(attr, string.Format(pager.UENCOREL,
pager.PageIndex+1), pager.NextPageText));//增多后一页的代码
sb.AppendFormat(GetAHtml(attr, string.Format(pager.UEnclaveL, pageNum),
pager.LastPageText));//增多最后一页的代码
if (pager.ShowSpanText)//是或不是出示分页文字span标签呈现
{
sb.AppendFormat(“<span class=\”” + pager.SpanTextClass +
“\”>共{0}页,每页{1}条记录 \n”, pageNum, pager.PageSize);
sb.AppendFormat(“到第<input type=\”input\” id=\”jumpNum\”
style=\”width:20px;\” name=\”jump\” value=\”{0}\” />页”,
pager.PageIndex == pageNum ? pageNum : pager.PageIndex + 1);
sb.AppendFormat(“<a href=\”#\” style=\”float:none;\”
onclick=\”javascript:jump();\”>” + pager.SubmitButtonText +
“</a></span>\n”);
sb.Append(GetJumpScript(pager.UQashqaiL));//增添按键跳转的javascript代码
}
sb.AppendFormat(“</div>”);//
return sb.ToString();
}

        /// <summary>
        /// get the html of a label
        /// </summary>
        /// <param name=”title”>a’s title</param>
        /// <param name=”url”>the url of a</param>
        /// <param name=”attr”>the attribute</param>
        /// <returns>return html string</returns>
        private static string GetAHtml(string attr,string url,string
title)
        {
            return “<a ” + attr + ” href=\””+url+”\”
style=\”margin-right:5px;\”>”+title+”</a>\n”;
        }

美高梅开户网址 12

3.天猫商城网珍宝列表页(

( 还会有个小小的的效率,点击去第几页的输入框时会弹出明显按钮),也隐含首页,只然则首页是用页码1代替。分页偏移量前后都以2页

/// <summary>
/// get the html of a label
/// </summary>
/// <param name=”title”>a’s title</param>
/// <param name=”url”>the url of a</param>
/// <param name=”attr”>the attribute</param>
/// <returns>return html string</returns>
private static string GetAHtml(string attr,string url,string title)
{
return “<a ” + attr + ” href=\””+url+”\”
style=\”margin-right:5px;\”>”+title+”</a>\n”;
}

        /// <summary>
        /// get the html of a label
        /// </summary>
        /// <param name=”num”>the content of span</param>
        /// <param name=”className”>Class style
Name</param>
        /// <returns>return html string </returns>
        private static string GetSpanHtml(int num, string className)
        {
            return “<span class=\”” + className + “\”>” + num +
“</span>\n”;
        }

美高梅开户网址 13

美高梅开户网址 14

/// <summary>
/// get the html of a label
/// </summary>
/// <param name=”num”>the content of span</param>
/// <param name=”className”>Class style Name</param>
/// <returns>return html string </returns>
private static string GetSpanHtml(int num, string className)
{
return “<span class=\”” + className + “\”>” + num +
“</span>\n”;
}

        /// <summary>
        /// 获取跳转的javascript代码
        /// </summary>
        /// <param name=”url”>当前分页的url准则</param>
        /// <returns>重临二个javascript代码</returns>
        private static string GetJumpScript(string url)
        {
            string scriptstr = “<script
type=\”text/javascript\”>\n” +
美高梅开户网址 ,                        “function jump(){\n” +
                            “var
jnum=document.getElementById(\”jumpNum\”).value;\n” +
                            “if(isNaN(jnum)){\n”+
                               
“alert(\”在跳转框中请输入数字!\”);\n” +
                                “}\n”+
                            “else{\n”+
                                //”alert(jnum);\n” +
                                “location.href=String.format(\”” + url

美高梅开户网址 15

美高梅开户网址 16

/// <summary>
/// 获取跳转的javascript代码
/// </summary>
/// <param name=”url”>当前分页的url准则</param>
/// <returns>重回二个javascript代码</returns>
private static string GetJumpScript(string url)
{
string scriptstr = “<script type=\”text/javascript\”>\n” +
“function jump(){\n” +
“var jnum=document.getElementById(\”jumpNum\”).value;\n” +
“if(isNaN{\n”+
“alert(\”在跳转框中请输入数字!\”);\n” +
“}\n”+
“else{\n”+
//”alert;\n” +
“location.href=String.format(\”” + url + “\”,jnum);\n” +
“}\n”+
“}\n”+
“String.format = function() {\n”+
“if( arguments.length == 0 )\n”+
“return null; \n”+
“var str = arguments[0]; \n”+
“for(var i=1;i<arguments.length;i++) {\n”+
“var re = new RegExp(‘\\\\{‘ + + ‘\\\\}’,’gm’);\n”+
“str = str.replace(re, arguments[i]);\n”+
“}\n”+
“return str;\n”+
“}\n”+
“</script>\n”;
return scriptstr;
}

  • “\”,jnum);\n” +
                                “}\n”+
                            “}\n”+
                            “String.format = function() {\n”+
                                “if( arguments.length == 0 )\n”+
                                    “return null; \n”+
                                “var str = arguments[0]; \n”+
                                “for(var i=1;i<arguments.length;i++)
    {\n”+
                                    “var re = new RegExp(‘\\\\{‘ + (i-1)
  • ‘\\\\}’,’gm’);\n”+
                                    “str = str.replace(re,
    arguments[i]);\n”+
                                “}\n”+
                                “return str;\n”+
                            “}\n”+
                    “</script>\n”;
                return scriptstr;
            }

 

美高梅开户网址 17

}

    }

3.Taobao网珍宝列表页(
 ),满含”上一页”、”下一页”、具体页码、消息、手动输入跳转至第几页
( 还应该有个非常的小的效果,点击去第几页的输入框时会弹出分明按键),也包涵首页,只可是首页是用页码1替代。分页偏移量前后都以2页

4.虾米列表(

… )、音信,也满含以页码1展现的首页。分页偏移量为前2页后5页

最精简须要的多少个参数字传送进去就会呈现分页效果了:
View Code
protected string str = “”;
protected void Page_Load(object sender, EventArgs e)
{
Pager pager = new Pager() { RecordCount = 350,
PageSize = 15,
MaxShowPageSize=10,
PageIndex = Convert.ToInt32(Request.QueryString[“page”]),
ShowSpanText=true};
str = SplitManager.AspNetPagers;

 
最精简须要的多少个参数字传送进去就能够显示分页效果了:
  View Code
protected string str = “”;
        protected void Page_Load(object sender, EventArgs e)
        {
            Pager pager = new Pager() { RecordCount = 350,
                PageSize = 15,
                MaxShowPageSize=10,
                PageIndex =
Convert.ToInt32(Request.QueryString[“page”]),
            ShowSpanText=true};
            str = SplitManager.AspNetPagers(pager);

 美高梅开户网址 18

美高梅开户网址 19

}

        }

美高梅开户网址 20

美高梅开户网址 21

仿csdn的分页的效应图

 
仿csdn的分页的法力图

 美高梅开户网址 22

美高梅开户网址 23

美高梅开户网址 24

美高梅开户网址 25

 

末段是织梦官方网址小说列表页(

供测试的css:
View Code
<style type=”text/css”>
/*分页样式调整的启幕*/
.paginator { font: 12px Arial, Helvetica, sans-serif;
padding:10px 20px 10px 0;
margin: 0px;}
.paginator a {border:solid 1px #ccc;
color:#0063dc;
cursor:pointer;
text-decoration:none;}
.paginator a:visited {padding: 1px 6px;
border: solid 1px #ddd;
background: #f0f1f1;
text-decoration: none;}
.paginator .cpb {border:1px solid #14316b;
font-weight:700;
color:#f0f1f1;
}
.paginator a:hover {border:solid 1px #14316b;
color:#14316b;
text-decoration:none;}
.paginator a,.paginator a:visited,.paginator .cpb,.paginator
a:hover{float:left;
height:16px;
line-height:16px;
min-width:10px;_width:10px;
margin-right:5px;
text-align:center;
white-space:nowrap;
font-size:12px;
font-family:
Arial,SimSun;
padding:0 3px;}
.paginator .stc{color:#999;margin-left:20px;}
.paginator .stc a{margin-left:10px;}
/*分页样式调控的终止*/
</style>

 

4.虾米列表(
 ),满含”上一页”、”下一页”、具体页码、可跳转的简短页码(
… )、消息,也囊括以页码1出示的首页。分页偏移量为前2页后5页

美高梅开户网址 26

况兼同盟为了协作分页,再交给一个DataTable转泛型列表的三个措施和二个分页存款和储蓄进度。
此调换方法需合营相应的实体类,况兼实体类中需对象相应表的字段名,不区分轻重缓急写。
View Code
#region DataTable To List/Model

供测验的css:
  View Code
<style type=”text/css”>
        /*分页样式调整的初步*/
.paginator { font: 12px Arial, Helvetica, sans-serif;
             padding:10px 20px 10px 0;
             margin: 0px;}
.paginator a {border:solid 1px #ccc;
              color:#0063dc;
              cursor:pointer;
              text-decoration:none;}
.paginator a:visited {padding: 1px 6px;
                      border: solid 1px #ddd;
                      background: #f0f1f1;
                      text-decoration: none;}
.paginator .cpb {border:1px solid #14316b;
                 font-weight:700;
                 color:#f0f1f1;
                 }
.paginator a:hover {border:solid 1px #14316b;
                    color:#14316b;
                    text-decoration:none;}
.paginator a,.paginator a:visited,.paginator .cpb,.paginator
a:hover{float:left;
                                                                    
height:16px;
                                                                    
line-height:16px;
                                                                    
min-width:10px;_width:10px;
                                                                    
margin-right:5px;
                                                                    
text-align:center;
                                                                    
white-space:nowrap;
                                                                    
font-size:12px;
                                                                    
font-family:
                                                                    
Arial,SimSun;
                                                                    
padding:0 3px;}
.paginator .stc{color:#999;margin-left:20px;}
.paginator .stc a{margin-left:10px;}
/*分页样式调整的告竣*/
    </style>
 

 美高梅开户网址 27

浏览至第11页时丰富可惜,宽度过宽导致版式出现难点:

/// <summary>
/// DataTable To List
/// </summary>
/// <typeparam name=”TType”>object type</typeparam>
/// <param name=”dt”>DataTable</param>
/// <returns>return a List Model type</returns>
public static List<T> DataTableToObjectList<T>(DataTable dt)
where T : new()
{
DataRowCollection drc = dt.Rows;
int columncount = drc.Count;
List<T> result = new List<T>(); //declare the generic type
of return
Type type = typeof;

而且相称为了合作分页,再交付三个DataTable转泛型列表的贰个主意和多个分页存款和储蓄进程。
此调换方法需协作相应的实体类,况且实体类中需对象相应表的字段名,不区分轻重缓急写。
  View Code
 #region DataTable To List/Model

美高梅开户网址 28

美高梅开户网址 29

PropertyInfo[] propertys =
type.GetProperties(BindingFlags.IgnoreCase|BindingFlags.Instance|BindingFlags.Public|BindingFlags.SetProperty);
//get the collections of the model
foreach (DataRow r in drc)
{
result.Add(DataRowToObjectModel<T>(r, propertys));
}
return result;

        /// <summary>
        /// DataTable To List
        /// </summary>
        /// <typeparam name=”TType”>object
type</typeparam>
        /// <param name=”dt”>DataTable</param>
        /// <returns>return a List Model type</returns>
        public static List<T>
DataTableToObjectList<T>(DataTable dt) where T : new()
        {
            DataRowCollection drc = dt.Rows;
            int columncount = drc.Count;
            List<T> result = new List<T>();    //declare the
generic type of return
            Type type = typeof(T);
           
            PropertyInfo[] propertys =
type.GetProperties(BindingFlags.IgnoreCase|BindingFlags.Instance|BindingFlags.Public|BindingFlags.SetProperty);  
//get the collections of the model
            foreach (DataRow r in drc)
            {
                result.Add(DataRowToObjectModel<T>(r,
propertys));
            }    
            return result;
           
        }
       
        /// <summary>
        /// DataRow To a Model
        /// </summary>
        /// <typeparam name=”T”>the type of
Model</typeparam>
        /// <param name=”r”>DataRow</param>
        /// <param name=”propertys”>the object to
Model</param>
        /// <returns>return a Model Type</returns>
        private static T DataRowToObjectModel<T>(DataRow r,
PropertyInfo[] propertys) where T : new()
        {
            T t = new T();
            for (int i = 0; i < propertys.Length; i++)
            {
                object obj = r[propertys[i].Name];
                if (obj != null)
                {
                    if (propertys[i].PropertyType == typeof(int))
                        propertys[i].SetValue(t,
PublicMethod.GetInt(obj), null);
                    if (propertys[i].PropertyType == typeof(string))
                        propertys[i].SetValue(t, obj.ToString(),
null);
                    if (propertys[i].PropertyType ==
typeof(DateTime))
                        propertys[i].SetValue(t,
PublicMethod.GetDateTime(obj), null);
                }
            }
            return t;
        }
        #endregion

美高梅开户网址 30

美高梅开户网址 31

}

分页存款和储蓄进程。
  View Code
CREATE PROCEDURE [dbo].[proc_SplitPage]
    — Add the parameters for the stored procedure here
    @tblName   varchar(255),       — 表名
    @strFields varchar(1000) = ‘*’, — 须要回到的列,私下认可*
    @strOrder varchar(255)=”,      — 排序的字段名,必填
    @strOrderType varchar(10)=’ASC’, — 排序的艺术,私下认可ASC
    @PageSize   int = 10,          — 页尺寸,默认10
    @PageIndex int = 1,           — 页码,默认1
    @strWhere varchar(1500) = ” — 查询条件 (注意: 不要加 where)
AS
declare @strSQL   varchar(5000)
if @strWhere !=”
set @strWhere=’ where ‘+@strWhere

 

那些分页功用的做法和作用是:

/// <summary>
/// DataRow To a Model
/// </summary>
/// <typeparam name=”T”>the type of Model</typeparam>
/// <param name=”r”>DataRow</param>
/// <param name=”propertys”>the object to Model</param>
/// <returns>return a Model Type</returns>
private static T DataRowToObjectModel<T>(DataRow r,
PropertyInfo[] propertys) where T : new()
{
T t = new T();
for (int i = 0; i < propertys.Length; i++)
{
object obj = r[propertys[i].Name];
if (obj != null)
{
if (propertys[i].PropertyType == typeof
propertys[i].SetValue(t, PublicMethod.GetInt, null);
if (propertys[i].PropertyType == typeof
propertys[i].SetValue(t, obj.ToString;
if (propertys[i].PropertyType == typeof)
propertys[i].SetValue(t, PublicMethod.GetDateTime, null);
}
}
return t;
}
#endregion

set @strSQL=
‘SELECT ‘+@strFields+’ FROM (‘+
    ‘SELECT ROW_NUMBER() OVER (ORDER
BY ‘+@strOrder+’ ‘+@strOrderType+’) AS pos,’+@strFields+’ ‘+
    ‘FROM ‘+@tblName+’ ‘+@strWhere+
‘) AS sp
WHERE pos BETWEEN ‘+str((@PageIndex-1)*@PageSize+1)+’ AND
‘+str(@PageIndex*@PageSize)

提及底是织梦官方网站小说列表页(
 ),富含了”首页”、”上一页”、”下一页”、具体页码、”末页”、下拉菜单选取跳转至第几页、新闻。分页偏移量前后都以5页:

1.url 分页 style1:

分页存款和储蓄进程。
View Code
CREATE PROCEDURE [dbo].[proc_SplitPage]
— Add the parameters for the stored procedure here
@tblName varchar, — 表名
@strFields varchar = ‘*’, — 供给重临的列,私下认可*
@strOrder varchar=”, — 排序的字段名,必填
@strOrderType varchar=’ASC’, — 排序的措施,暗中认可ASC
@PageSize int = 10, — 页尺寸,默认10
@PageIndex int = 1, — 页码,默认1
@strWhere varchar = ” — 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar
if @strWhere !=”
set @strWhere=’ where’+@strWhere

exec (@strSQL)

美高梅开户网址 32

①手动输入跳转页码的章程:

set @strSQL=
‘SELECT’+@strFields+’FROM (‘+
‘SELECT ROW_NUMBER() OVER (ORDER BY’+@strOrder+”+@strOrderType+’) AS
pos,’+@strFields+’ ‘+
‘FROM’+@tblName+”+@strWhere+
‘) AS sp
WHERE pos BETWEEN ‘+str((@PageIndex-1)*@PageSize+1)+’ AND
‘+str(@PageIndex*@PageSize)

 
以上是全体代码,由于本身还是新人,请私家查找难点并引导,多谢。

浏览至第11页时有十分的多意见,宽度过宽导致版式出现难点: 

一直呈现最终一页

exec

 美高梅开户网址 33

美高梅开户网址 34

上述是整整代码,由于本身只怕新人,请私家查找难题并教导,谢谢。

美高梅开户网址 35

“…”跳转至 当前展现的除末页的最大页码的下一页,鼠标放在上边给出提醒

 

美高梅开户网址 36

本条分页成效的做法和效应是:

上下偏移量可自定义,可同等可比不上,前边的”…”跳转至最前页除首页的页码的前一页

1.url 分页 style1:

美高梅开户网址 37

①手动输入跳转页码的章程:

②下拉菜单采取跳转的格局:

一味突显最终一页

美高梅开户网址 38

 美高梅开户网址 39

2.url 分页 style2:

“…”跳转至 当前展现的除末页的最大页码的下一页,鼠标放在下边给出提醒

选择”首页”和”末页”代替页码”1″和末段一页页码,使用前n页、后n页代替”…”

美高梅开户网址 40

美高梅开户网址 41

内外偏移量可自定义,可同等可不及,前面包车型大巴”…”跳转至最前页除首页的页码的前一页

为了使”前10页”和”后10页”同一时候出现,扩展了数据库的数量

美高梅开户网址 42

美高梅开户网址 43

 

平等有下拉菜单跳转格局

②下拉菜单接纳跳转的法子:

美高梅开户网址 44

美高梅开户网址 45

3.ajax 分页:

2.url 分页 style2:

出现的要素独有”首页”、”上一页”、”下一页”和”末页”。

运用”首页”和”末页”代替页码”1″和终极一页页码,使用前n页、后n页代替”…”

首页时:

美高梅开户网址 46

美高梅开户网址 47

为了使”前10页”和”后10页”相同的时候出现,扩大了数据库的多寡

中间时:

美高梅开户网址 48

美高梅开户网址 49

同等有下拉菜单跳转方式

末页时:

美高梅开户网址 50

美高梅开户网址 51

 

模块的文件结构图:

3.ajax 分页:

ROOT:
├─conn
│ └─conn.php

├─libs — smarty库

├─templates
│ │
│ ├─demo.html — 功用页模板文件
│ │
│ ├─css
│ │ ├─common.css
│ │ └─style1.css
│ │
│ ├─images
│ │ └─loading.gif — ajax分页时乞求数据接收到之前的加载图
│ └─js
│ ├─jquery-1.8.3.min.js
│ └─ajax.js — 当分页格局为ajax时模板demo.html加载的js

├─templates_c

├─init.inc.php — smarty配置文件

├─page.class.php — 分页类

├─demo.php

└─ajaxpage.php — ajax分页时接受诉求的php文件

并发的要素独有”首页”、”上一页”、”下一页”和”末页”。

要专注的地点:

首页时:

1.偏移量的显得设置,首假如哪些时候 url 分页形式1,哪天显得”…”
:当前页码 – 前偏移量 – 1 > 1 时,应当突显前边的”…”; 当前页码 +
后偏移量 + 1 < 总页数时,应当呈现后边的”…”;

美高梅开户网址 52

2.接纳性加载 js :当使用 ajax 方式实行分页时,才加载 ajax.js

中间时:

3.表面包车型客车 js 不能剖判 smarty 的价签,所以在使用外界 js 时的传值要实行管理

美高梅开户网址 53

4.ajax 分页时,私下认可是第一页,约等于分明首先会产出 “下一页” 和
“末页”,所以 “上一页” 和 “首页” 的丰硕和点击函数应当包涵在”下一页” 和
“末页” 的点击函数中。

末页时:

驷不比舌代码:

美高梅开户网址 54

page.class.php:

 

<?php

class MyPageUrl{

  private $totalNum;
  private $perpageNum;    //每页显示条数  
  private $pageNow;      //当前页页码
  private $url;        //当前url


  //页码显示
  private $pageStyle; //页码样式,提供2种样式

  private $prePage;    //页码前偏移量
  private $floPage;    //页码后偏移量

  private $skipStyle;    //手动跳转,0为手动输入页码,1为下拉菜单选择页码

  private $page_act;    //翻页样式 0:url 1:ajax

  //页码文字
  //style2&style3
  private $firstFonts = "首页";
  private $lastFonts = "末页";

  private $nextFonts = "下一页 >";    
  private $preFonts = "< 上一页";

  //前n页,后n页
  private $page_n;
  private $pn = 10;
  private $pn_fonts = "前10页";
  private $fn = 10;
  private $fn_fonts = "后10页";

  //展现的页码
  private $pageShow;

  //构造函数
  function __construct($totalNum,$perpageNum,$prePage,$preFonts,$floPage,$nextFonts,$p,$skipStyle,$pageStyle,$page_n,$page_act){

    $this->totalNum = $totalNum;
    $this->perpageNum = $perpageNum;
    $this->prePage = $prePage;
    $this->floPage = $floPage;
    $this->skipStyle = $skipStyle;
    $this->pageStyle = $pageStyle;
    $this->page_n = $page_n;
    $this->page_act = $page_act;

    $this->getPageNow($p);

    $this->totalPage = $this->getTotalPage(); //总页数

    $this->pageShow = "";
    $this->getUrl();
  }


  /************定义__toString方法,把对象解析成字符串******/
  public function __toString(){

    return $this->pageShow;
  }

  /************获得当前页页码,$p用来接收$_GET['p']*******/
  public function getPageNow($p){

    if(!isset($p)){

      $this->pageNow = 1;
    }else if($p>0){

      $this->pageNow = $p;  
    }else{

      die("page number error");
    }

    return $this->pageNow;
  }



  /***********************设置当前页面链接***************/
  public function getUrl(){

    $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

    //判断是否带参数
    if(strpos($url,"?") === false){ //不带参数

      return $this->url = $url."?";
    }else{ //带参数

      $url = explode("?",$url);
      //参数
      $param = $url[1];

      //判断是否有多个参数
      if(strpos($param,"&") === false){ //只有一个参数

        //判断参数是否为p
        if(strpos($param,"p=") === false){ //不含参数p

          //合并url
          $url = implode("?",$url);  

          return $this->url = $url."&";

        }else{

          //把参数p去掉
          $url = $url[0];

          return $this->url = $url."?";
        }

      }else{ //多个参数

        $param = explode("&",$param);

        //遍历参数数组
        foreach($param as $k=>$v){

          if(strpos($v,"p=") === false){

            continue;
          }else{

            //当含有参数p时,把它从数组中删除
            unset($param[$k]);
          }
        }

          //删除参数p之后组合数组
          $param = implode("&",$param);
          $url[1] = $param;
          $url = implode("?",$url);

          return $this->url = $url."&";
      }
    }
  }

  /************************前偏移量处理********************/
  public function preOffset($preFonts){


    //前偏移量的处理
    if($this->pageNow!=1 && ($this->pageNow - $this->prePage -1 <= 1)){

      //上一页
      $this->pageShow .= "<a id=\"per_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-1)."\">".($preFonts == ""?$this->preFonts:$preFonts)."</a>";


      //页码
      for($i=1;$i<=$this->pageNow-1;$i++){    

        //ajax方式不显示
        if($this->page_act != 1){

          $this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".$i."\">".$i."</a>";  
        }
      }

    }else if($this->pageNow - $this->prePage -1 > 1){ //pageNow至少大于2时才会出现"1..."

      //样式1.加上'首页'
      if($this->pageStyle == 2 || $this->page_act == 1){

        //首页
        $this->pageShow .= "<a id=\"first_page\" class=\"pagenum\" href=\"".$this->url."p=1\">".$this->firstFonts."</a>";

        //style2.前n页
        if($this->page_n == 1 && $this->page_act != 1){

          if($this->pageNow>$this->pn){

            $this->pageShow .= "<a id=\"pre_n_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-$this->pn)."\">".$this->pn_fonts."</a>";
          }
        }
      }


      //上一页
      $this->pageShow .= "<a id=\"pre_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-1)."\">".($preFonts == ""?$this->preFonts:$preFonts)."</a>";

      //样式1.加上第一页'1'、'...'
      if($this->pageStyle == 1){
        $this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."\">1</a><a id=\"pre_page_2\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-$this->prePage-1)." \" title=\"第".($this->pageNow-$this->prePage-1)."页\">…</a>";
      }

      for($i=$this->prePage;$i>=1;$i--){    

        //当前页和'...'之间的页码,ajax方式不显示
        if($this->page_act != 1){

          $this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-$i)."\">".($this->pageNow-$i)."</a>";  
        }
      }
    }
  }

  /**********************页码和后偏移量处理***************************/
  public function floOffset($nextFonts){

    if($this->totalPage > $this->floPage){ //总页数大于后偏移量时

      for($i=0;$i<=$this->floPage;$i++){

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

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

          //页码,ajax方式不显示
          if($this->page_act != 1){

            $this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".$page."\">".$page."</a>";
          }
        }
      }

      if($this->pageNow < $this->totalPage){


        //当前页+后偏移量+1小于总页数时出现"..."
        if(($this->pageNow+$this->floPage+1)<$this->totalPage){

          //样式1.显示'...'
          if($this->pageStyle == 1){
            $this->pageShow .= "<a id=\"flo_page_2\" class=\"pagenum\" href=\"".$this->url."p=".($page+1)."\" title=\"第".($page+1)."页\">…</a>";
          }
        }


        //当前页+后偏移量+1小于等于总页数时出现最后一页的快捷标签
        if(($this->pageNow+$this->floPage+1)<=$this->totalPage){

          //最后一页
          //样式1.始终出现'最后一页页码'
          if($this->pageStyle == 1){
            $this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".$this->totalPage."\" title=\"总共".$this->totalPage."页\">".$this->totalPage."</a>";
          }
        }            

        $this->pageShow .= "<a id=\"flo_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow+1)."\">".($nextFonts == ""?$this->nextFonts:$nextFonts)."</a>"; //当实例化对象时用户传递的文字为空时则调用类预设的"下一页",否则输出用户传递的值

        //style2.加上后n页
        if($this->pageStyle == 2 && $this->page_n == 1 && $this->page_act != 1){

          if(($this->pageNow+10)<$this->totalPage){

            $this->pageShow .= "<a id=\"flo_n_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow+$this->fn)."\">".$this->fn_fonts."</a>";
          }
        }

        //显示'末页'
        if($this->pageStyle == 2){

          if(($this->pageNow+$this->floPage+1)<$this->totalPage){

            $this->pageShow .= "<a id=\"last_page\" class=\"pagenum\" href=\"".$this->url."p=".$this->totalPage."\">末页</a>";
          }
        }

      }else if($this->pageNow > $this->totalPage){

        die("超出页码范围");
      }

    }else{ //总页数小于后偏移量时

      for($i=0;$i<$this->totalPage;$i++){

        $page = $this->pageNow+$i;
        //页码后边界
        $this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".$page."\">".$page."</a>";
      }
    }
  }

  /********************其它页面信息***********************/
  public function getOtherInfo(){

    //ajax方式不显示手动跳转信息
    if($this->page_act != 1){

      $this->pageShow .= " 跳转至 ";

      //跳转类型
      if($this->skipStyle =="" ){ //不加跳转

        $this->pageShow .= "";
      }else if($this->skipStyle == 1){ //输入框

        $this->pageShow .= "<input id=\"skip\" type=\"text\" value=\"".$this->pageNow."\">";

        $this->pageShow .= "<button id=\"go\">GO</button>";
      }else if($this->skipStyle == 2){ //下拉菜单

        //选择下拉菜单自动跳转
        $this->pageShow .= "<select id=\"select_page\" onchange=\"location.href=this.options[this.selectedIndex].value;\" >";

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

          $this->pageShow .= "<option value=\"".$this->url."p=".$i."\""; 

          //下拉菜单默认显示当前页
          if($i == $this->pageNow){

            $this->pageShow .= " selected";
          }

          $this->pageShow .= ">".$i."</option>";
        }

        $this->pageShow .= "</select>";
      }
    }

    $this->pageShow .= "  当前第".$this->pageNow."页";
    $this->pageShow .= "/共".$this->totalPage."页";
    $this->pageShow .= " 共".$this->totalNum."条";
  }


  /*****************获取每页第一条信息*****************/
  public function getFirstRow(){

    $firstRow = $this->perpageNum * ($this->pageNow-1) + 1;//当前页第一条是总条数中第几条

    return $firstRow;
  }

  /********************获得总页数***********************/
  public function getTotalPage(){

    $totalPage = ceil($this->totalNum / $this->perpageNum);
    return $totalPage;
  }

  /* ****************获取上一页、下一页文字*************** */

  public function getPreFonts($preFonts){

      return ($preFonts=="")?$this->preFonts:$preFonts;
  }

  public function getNextFonts($nextFonts){

      return ($nextFonts=="")?$this->nextFonts:$nextFonts;
  } 

}

模块的公文结构图:

demo.php:

ROOT:
├─conn
│    └─conn.php

├─libs               — smarty库

├─templates
│ │
│ ├─demo.html  — 成效页模板文件
│ │
│ ├─css
│ │    ├─common.css
│ │    └─style1.css
│ │
│ ├─images
│ │      └─loading.gif            —
ajax分页时央浼数据接受到在此以前的加载图
│ └─js
│     ├─jquery-1.8.3.min.js
│     └─ajax.js                   —
当分页格局为ajax时模板demo.html加载的js

├─templates_c

├─init.inc.php                  — smarty配置文件

├─page.class.php             — 分页类

├─demo.php

└─ajaxpage.php               — ajax分页时接受央求的php文件

<?php

require 'init.inc.php';
require 'page.class.php';
require 'conn/conn.php';

$perpageNum = 10; //每页数据条数
$perPage = 4; //前分页偏移量
$floPage = 4; //后分页偏移量
$preFonts = ""; //"前一页"文字内容
$nextFonts = ""; //"下一页"文字内容
$page_n = 1; //样式2下是否加"前n页"、后n页,0为不加,1为加
$skipStyle = 1; //跳转类型,可选1、2
$pageStyle = 1; //样式类型,可选1、2、3( 样式3只包含"上一页"、"下一页"和页码 )
$page_act = 0; //0:url 和 1:ajax

if($page_act == 1){

  //ajax方式分页时强制使用第二种样式
  $pageStyle = 2;
}


$p = isset($_GET['p'])?$_GET['p']:1; //当前页码

//在page.class.php中定义__toString方法,把对象$mypage解析成字符串输出

//参数分别是:总条数、每页条数、前偏移量、"上一页"文字内容(默认为""时显示"上一页")、后偏移量、"下一页"文字内容(默认为""时显示"下一页")、当前地址栏页码数、手动跳转样式、页码显示样式、样式2是否加前n页后n页、分页方式(url/ajax)

//获得总条数
//输出列表
$sql_all = "select title from ips_archives";

//总条数
$totalNum = $conne->getRowsNum($sql_all);

//实例化
$mypageurl = new MyPageUrl($totalNum,$perpageNum,$perPage,$preFonts,$floPage,$nextFonts,$p,$skipStyle,$pageStyle,$page_n,$page_act);


//每页第一条
$firstRow = $mypageurl->getFirstRow();

//总条数
$totalPage = $mypageurl->getTotalPage();

//输出列表
$sql = "select title from ips_archives order by pubdate desc limit ".$firstRow.",".$perpageNum;

//取出数据(二维数组)
$rowsArray = $conne->getRowsArray($sql);

//显示页码
$pageShow = $mypageurl->preOffset($preFonts).$mypageurl->floOffset($nextFonts).$mypageurl->getOtherInfo();

$smarty->assign("Template_Dir",Template_Dir);
$smarty->assign("page_act",$page_act); //传递分页方式
$smarty->assign("pageNow",$p); //传递当前页
$smarty->assign("perpageNum",$perpageNum); //传递每页几条数据
$smarty->assign("totalPage",$totalPage); //传递总页数
$smarty->assign("preFonts",$mypageurl->getPreFonts($preFonts)); //传递上一页文字信息
$smarty->assign("rowsArray",$rowsArray);
$smarty->assign("mypage",$mypageurl);
$smarty->display("demo.html");

 

利用方法在demo.php的笺注里

要小心的地点:

demo.html:

1.偏移量的来得设置,首如果怎么时候 url 分页形式1,何时显得”…”
:当前页码 – 前偏移量 – 1 > 1 时,应当展现前边的”…”; 当前页码 +
后偏移量 + 1 < 总页数时,应当展现前边的”…”;

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PHP分页类</title>
<link href="<{$Template_Dir}>/css/common.css" rel="stylesheet" type="text/css">
<link href="<{$Template_Dir}>/css/style1.css" rel="stylesheet" type="text/css">
<script id="jq" src="<{$Template_Dir}>/js/jquery-1.8.3.min.js"></script>
</head>
<body>

  <div id="list">

    <ul id="newsul">

      <{foreach $rowsArray as $val}>
        <li><{$val['title']}></li>
      <{/foreach}>

    </ul>

  </div>
  <div id="page"><{$mypage}></div>
  <input id="pageNow" type="hidden" value="<{$pageNow}>">
  <!--分页方式-->
  <input id="page_act" type="hidden" value="<{$page_act}>">
  <!--每页几条数据-->
  <input id="perpageNum" type="hidden" value="<{$perpageNum}>">
  <!--总页数-->
  <input id="totalPage" type="hidden" value="<{$totalPage}>">
  <!--//把smarty的变量传递给外部js-->
  <input id="Template_Dir" type="hidden" value="<{$Template_Dir}>">
  <input id="preFonts" type="hidden" value="<{$preFonts}>">
</body>
<script>

  $(function(){

    //遍历a
    $(".pagenum").each(function(){

      if($(this).text() == $("#pageNow").val()){

        $(this).addClass("selected");
      }
    });

    //如果存在跳转输入框
    if($("#skip").length>0){

      $("#skip").keydown(function(){

        if(event.keyCode == 13){ //回车

          self.location="demo.php?p="+$(this).val();
        }
      });
    }

    //点击"GO"按钮跳转
    if($("#go").length>0){

      $("#go").click(function(){

        self.location="demo.php?p="+$("#skip").val();
      });
    }

    //如果分页方式是ajax,则加载外部ajax.js
    if($("#page_act").val() == 1){

      //把smarty的变量传递给外部js
      $Template_Dir = $("#Template_Dir").val();
      $preFonts = $("#preFonts").val();

      $insertAjax = $("<script src=\"<{$Template_Dir}>/js/ajax.js\"><\/script>");
      $insertAjax.insertAfter($("#jq"));
    }

    //最后一行row去掉border-bottom
    $("#list ul").children("li:last").css("border-bottom",0);
  });
</script>
</html>

2.采用性加载 js :当使用 ajax 格局开展分页时,才加载 ajax.js

ajaxpage.php:

3.表面的 js 不能够解析 smarty 的价签,所以在行使外界 js 时的传值要进行拍卖

<?php

require 'conn/conn.php';

if(isset($_POST['pageNow']) && !empty($_POST['pageNow'])){

  $pageNow = $_POST['pageNow'];
}

//每页几条数据
if(isset($_POST['perpageNum']) && !empty($_POST['perpageNum'])){

  $perpageNum = $_POST['perpageNum'];
}

//当前页第一条数据
$firstRow = $perpageNum * ($pageNow-1) + 1;

$sql = "select title from ips_archives order by pubdate desc limit ".$firstRow.",".$perpageNum;


$rowsArray = $conne->getRowsArray($sql);

//把二维数组转换成json格式
echo json_encode($rowsArray);

4.ajax 分页时,暗中认可是首先页,约等于不容争辩首先会油不过生 “下一页” 和
“末页”,所以 “上一页” 和 “首页” 的增进和点击函数应当涵盖在”下一页” 和
“末页” 的点击函数中。

ajax.js:

 

//删除原先的li,插入gif
function ajaxpre(){

  //删除原先的title
  $("#newsul li").remove();

  //插入gif图
  $loading = $("<img class=\"loading\" src=\""+$Template_Dir+"/images/loading.gif\">");

  $loading.appendTo($("#newsul"));
}

//隐藏翻页信息
function infoAct(){

  //当前页到达尾页时,"下一页"和"末页"
  if(parseInt($("#pageNow").val()) == parseInt($("#totalPage").val())){

    $("#flo_page").hide();
    $("#last_page").hide();

    $("#pre_page").show();
    $("#first_page").show();

  }else if(parseInt($("#pageNow").val()) == 1){ //当前页到达时隐藏"首页"和"上一页"

    $("#pre_page").hide();
    $("#first_page").hide();

    $("#flo_page").show();
    $("#last_page").show();
  }else{

    if($("#pre_page").is(":hidden") || $("#pre_page").length == 0){
      $("#pre_page").show();
    }
    if($("#first_page").is(":hidden") || $("#first_page").length == 0){
      $("#first_page").show();
    }
    if($("#flo_page").is(":hidden") || $("#flo_page") == 0){
      $("#flo_page").show();
    }
    if($("#last_page").is(":hidden") || $("#last_page").length == 0){
      $("#last_page").show();
    }
  }
}

//点击"下一页"、"末页"时出现"首页"和"上一页"
function showPage(){

  //首页
  $firstPage = $("<a id=\"first_page\" class=\"pagenum\">首页</a>");

  if($("#first_page").length == 0){
    $firstPage.insertBefore($("#flo_page"));
  }

  //上一页
  $pre_page = $("<a id=\"pre_page\" class=\"pagenum\">"+$preFonts+"</a>");

  if($("#pre_page").length == 0){
    $pre_page.insertBefore($("#flo_page"));
  }
}

//ajax请求数据
function ajaxpost(){

  $.post("ajaxpage.php",{

    pageNow : parseInt($("#pageNow").val()),
    perpageNum : parseInt($("#perpageNum").val())
  },function(data,textStatus){

    //接收json数据
    var dataObj=eval("("+data+")"); //转换为json对象 

    //删除gif
    $(".loading").remove();

    $.each(dataObj,function(idx,item){ 

      $li_new = $("<li>"+item.title+"</li>");
      $li_new.appendTo($("#newsul"));
    })
    $("#list ul").children("li:last").css("border-bottom",0);
  });
}


//初始值=1
apagenow = parseInt($("#pageNow").val());

//ajax "首页" 因为"首页"和"上一页"一开始是不出现的,所以只有在"下一页"和"末页"的的点击函数中调用"首页"和"上一页"函数
function firstPageAct(){

  if($("#first_page").is(":visible")){

    $("#first_page").click(function(){

      //删除更新前的
      ajaxpre();

      //pageNow设为1
      $("#pageNow").val(1);
      apagenow = parseInt($("#pageNow").val());

      //修改页码信息
      $("#pagenow_info").html("  当前第1页");

      //ajax请求数据
      ajaxpost();

      //到达"首页"之后隐藏"首页"和"上一页"
      infoAct();
    });
  }
}

//ajax "上一页"
function prePageAct(){

  if($("#pre_page").is(":visible")){

    $("#pre_page").click(function(){

      //删除更新前的
      ajaxpre();

      //每点击"下一次",隐藏域值-1
      if(parseInt(apagenow) != 1){

        apagenow = parseInt(apagenow) - parseInt(1);
      }

      $("#pageNow").val(apagenow);

      //隐藏域的页码值大于1时
      if(parseInt($("#pageNow").val()) > parseInt(1)){

        //修改页码信息
        $("#pagenow_info").html("  当前第"+$("#pageNow").val()+"页");
      }

      //ajax请求数据
      ajaxpost();

      //第一页时隐藏"首页"和"下一页"
      infoAct();
    });

  }
}

//ajax "下一页"
if($("#flo_page").length>0){

  //去掉a的href属性
  $("#flo_page").removeAttr("href");

  $("#flo_page").click(function(){

    ajaxpre();

    //每点击"下一次",隐藏域值+1
    apagenow = parseInt(apagenow) + parseInt(1);

    $("#pageNow").val(apagenow);

    //隐藏域的页码值小于总页码时
    if(parseInt($("#pageNow").val()) <= parseInt($("#totalPage").val())){

      //修改页码信息
      $("#pagenow_info").html("  当前第"+$("#pageNow").val()+"页");

      //ajax请求数据
      ajaxpost();
    }

    //点击"下一页"之后出现"首页"
    if($("#first_page").is(":hidden") || $("#first_page").length == 0){

      //出现"首页"和"下一页"
      showPage();
      firstPageAct();
      prePageAct();
    }

    //隐藏"下一页"和"末页"
    infoAct();

    return false; //取消点击翻页
  });
}

//ajax "末页"
if($("#last_page").length>0){

  //去掉a的href属性
  $("#last_page").removeAttr("href");

  $("#last_page").click(function(){

    ajaxpre();

    //修改隐藏域当前页信息
    apagenow = parseInt($("#totalPage").val());
    $("#pageNow").val(apagenow);

    //修改页码信息
    $("#pagenow_info").html("  当前第"+$("#totalPage").val()+"页");

    //ajax请求数据
    ajaxpost();

    //点击"末页"之后出现"首页"

    if($("#first_page").length == 0){

      showPage();
      firstPageAct();
      prePageAct();
    }

    infoAct();

    return false;
  });
}



//取消a标签跳转
$("#first_page").click(function(){

  return false;
});

$("#pre_page").click(function(){

  return false;
});

重大代码:

common.css:

page.class.php:

a{ font-size:14px; font-family:Tahoma; color:#444; text-decoration:none; cursor:pointer;}
ul{ list-style-type:none;}

/* ***************************列表样式******************** */
#list{

  width:1000px;
  margin-bottom:20px;
  border:1px solid #95071b;
}

#list ul{

  margin:5px 20px;
  padding:0px;
}

#list li{

  line-height:24px;
  border-bottom:1px solid #95071b;
}

/* ****************************跳转样式******************** */
#skip{

  width:36px;
  text-align:center;
}

/* ****************************ajax************************* */
.loading{

  position:absolute;
  top:20%;
  left:35%;
}

美高梅开户网址 55美高梅开户网址 56

style1.css:

  1 <?php    2     3 class MyPageUrl{    4     5     private $totalNum;    6     private $perpageNum;        //每页显示条数        7     private $pageNow;            //当前页页码    8     private $url;                //当前url    9    10        11     //页码显示   12     private $pageStyle; //页码样式,提供2种样式   13    14     private $prePage;        //页码前偏移量   15     private $floPage;        //页码后偏移量   16    17     private $skipStyle;        //手动跳转,0为手动输入页码,1为下拉菜单选择页码   18    19     private $page_act;        //翻页样式 0:url 1:ajax   20    21     //页码文字   22     //style2&style3   23     private $firstFonts = "首页";   24     private $lastFonts = "末页";   25    26     private $nextFonts = "下一页 >";           27     private $preFonts = "< 上一页";   28    29     //前n页,后n页   30     private $page_n;   31     private $pn = 10;   32     private $pn_fonts = "前10页";   33     private $fn = 10;   34     private $fn_fonts = "后10页";   35    36     //展现的页码   37     private $pageShow;   38    39     //构造函数   40     function __construct($totalNum,$perpageNum,$prePage,$preFonts,$floPage,$nextFonts,$p,$skipStyle,$pageStyle,$page_n,$page_act){   41        42         $this->totalNum = $totalNum;   43         $this->perpageNum = $perpageNum;   44         $this->prePage = $prePage;   45         $this->floPage = $floPage;   46         $this->skipStyle = $skipStyle;   47         $this->pageStyle = $pageStyle;   48         $this->page_n = $page_n;   49         $this->page_act = $page_act;   50    51         $this->getPageNow($p);   52    53         $this->totalPage = $this->getTotalPage(); //总页数   54    55         $this->pageShow = "";   56         $this->getUrl();   57     }   58                59    60     /************定义__toString方法,把对象解析成字符串******/   61     public function __toString(){   62        63         return $this->pageShow;   64     }   65    66     /************获得当前页页码,$p用来接收$_GET['p']*******/   67     public function getPageNow($p){   68        69         if(!isset($p)){   70                71             $this->pageNow = 1;   72         }else if($p>0){   73                74             $this->pageNow = $p;       75         }else{   76            77             die("page number error");   78         }   79    80         return $this->pageNow;   81     }   82    83    84    85     /***********************设置当前页面链接***************/   86     public function getUrl(){   87    88         $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];   89            90         //判断是否带参数   91         if(strpos($url,"?") === false){ //不带参数   92    93             return $this->url = $url."?";   94         }else{ //带参数   95            96             $url = explode("?",$url);   97             //参数   98             $param = $url[1];   99   100             //判断是否有多个参数  101             if(strpos($param,"&") === false){ //只有一个参数  102       103                 //判断参数是否为p  104                 if(strpos($param,"p=") === false){ //不含参数p  105                   106                     //合并url  107                     $url = implode("?",$url);      108   109                     return $this->url = $url."&";  110   111                 }else{  112                   113                     //把参数p去掉  114                     $url = $url[0];  115   116                     return $this->url = $url."?";  117                 }  118                   119             }else{ //多个参数  120               121                 $param = explode("&",$param);  122   123                 //遍历参数数组  124                 foreach($param as $k=>$v){  125   126                     if(strpos($v,"p=") === false){  127                           128                         continue;  129                     }else{  130               131                         //当含有参数p时,把它从数组中删除  132                         unset($param[$k]);  133                     }  134                 }  135   136                     //删除参数p之后组合数组  137                     $param = implode("&",$param);  138                     $url[1] = $param;  139                     $url = implode("?",$url);  140   141                     return $this->url = $url."&";  142             }  143         }  144     }  145   146     /************************前偏移量处理********************/  147     public function preOffset($preFonts){  148       149   150         //前偏移量的处理  151         if($this->pageNow!=1 && ($this->pageNow - $this->prePage -1 <= 1)){  152                       153             //上一页  154             $this->pageShow .= "<a id=\"per_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-1)."\">".($preFonts == ""?$this->preFonts:$preFonts)."</a>";  155   156               157             //页码  158             for($i=1;$i<=$this->pageNow-1;$i++){          159   160                 //ajax方式不显示  161                 if($this->page_act != 1){  162   163                     $this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".$i."\">".$i."</a>";      164                 }  165             }  166   167         }else if($this->pageNow - $this->prePage -1 > 1){ //pageNow至少大于2时才会出现"1..."  168               169             //样式1.加上'首页'  170             if($this->pageStyle == 2 || $this->page_act == 1){  171                   172                 //首页  173                 $this->pageShow .= "<a id=\"first_page\" class=\"pagenum\" href=\"".$this->url."p=1\">".$this->firstFonts."</a>";  174   175                 //style2.前n页  176                 if($this->page_n == 1 && $this->page_act != 1){  177                   178                     if($this->pageNow>$this->pn){  179                       180                         $this->pageShow .= "<a id=\"pre_n_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-$this->pn)."\">".$this->pn_fonts."</a>";  181                     }  182                 }  183             }  184               185               186             //上一页  187             $this->pageShow .= "<a id=\"pre_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-1)."\">".($preFonts == ""?$this->preFonts:$preFonts)."</a>";  188               189             //样式1.加上第一页'1'、'...'  190             if($this->pageStyle == 1){  191                 $this->pageShow .=  "<a class=\"pagenum\" href=\"".$this->url."\">1</a><a id=\"pre_page_2\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-$this->prePage-1)." \" title=\"第".($this->pageNow-$this->prePage-1)."页\">…</a>";  192             }  193   194             for($i=$this->prePage;$i>=1;$i--){          195   196                 //当前页和'...'之间的页码,ajax方式不显示  197                 if($this->page_act != 1){  198   199                     $this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow-$i)."\">".($this->pageNow-$i)."</a>";      200                 }  201             }  202         }  203     }  204   205     /**********************页码和后偏移量处理***************************/  206     public function floOffset($nextFonts){  207       208         if($this->totalPage > $this->floPage){ //总页数大于后偏移量时  209   210             for($i=0;$i<=$this->floPage;$i++){  211               212                 $page = $this->pageNow+$i;  213                   214                 if($page<=$this->totalPage){  215   216                     //页码,ajax方式不显示  217                     if($this->page_act != 1){  218   219                         $this->pageShow .= "<a class=\"pagenum\" href=\"".$this->url."p=".$page."\">".$page."</a>";  220                     }  221                 }  222             }  223   224             if($this->pageNow < $this->totalPage){  225   226                   227                 //当前页+后偏移量+1小于总页数时出现"..."  228                 if(($this->pageNow+$this->floPage+1)<$this->totalPage){  229               230                     //样式1.显示'...'  231                     if($this->pageStyle == 1){  232                         $this->pageShow .= "<a id=\"flo_page_2\" class=\"pagenum\" href=\"".$this->url."p=".($page+1)."\" title=\"第".($page+1)."页\">…</a>";  233                     }  234                 }  235                   236                   237                 //当前页+后偏移量+1小于等于总页数时出现最后一页的快捷标签  238                 if(($this->pageNow+$this->floPage+1)<=$this->totalPage){  239                   240                     //最后一页  241                     //样式1.始终出现'最后一页页码'  242                     if($this->pageStyle == 1){  243                         $this->pageShow .= "<a id=\"flo_page\" class=\"pagenum\" href=\"".$this->url."p=".$this->totalPage."\" title=\"总共".$this->totalPage."页\">".$this->totalPage."</a>";  244                     }  245                 }                          246   247                 $this->pageShow .= "<a id=\"flo_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow+1)."\">".($nextFonts == ""?$this->nextFonts:$nextFonts)."</a>"; //当实例化对象时用户传递的文字为空时则调用类预设的"下一页",否则输出用户传递的值  248   249                 //style2.加上后n页  250                 if($this->pageStyle == 2 && $this->page_n == 1 && $this->page_act != 1){  251                   252                     if(($this->pageNow+10)<$this->totalPage){  253                       254                         $this->pageShow .= "<a id=\"flo_n_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow+$this->fn)."\">".$this->fn_fonts."</a>";  255                     }  256                 }  257   258                 //显示'末页'  259                 if($this->pageStyle == 2){  260                   261                     if(($this->pageNow+$this->floPage+1)<$this->totalPage){  262   263                         $this->pageShow .= "<a id=\"last_page\" class=\"pagenum\" href=\"".$this->url."p=".$this->totalPage."\">末页</a>";  264                     }  265                 }  266   267             }else if($this->pageNow > $this->totalPage){  268               269                 die("超出页码范围");  270             }  271         }else{ //总页数小于后偏移量时  272               273             if($this->pageNow < $this->totalPage){  //当前页小于总页数时  274   275                 for($i=0;$i<$this->totalPage;$i++){  276               277                     $page = $this->pageNow+$i;  278                       279                     if($page < $this->totalPage){  280                       281                         if($this->page_act != 1){  282   283                             //页码后边界  284                             $this->pageShow .= "<a id=\"flo_page\" class=\"pagenum\" href=\"".$this->url."p=".$page."\">".$page."</a>";  285                         }  286   287                     }else if($page == $this->totalPage){  288                       289                         if($this->page_act != 1){  290   291                             $this->pageShow .= "<a id=\"flo_page\" class=\"pagenum\" href=\"".$this->url."p=".$page."\">".$page."</a>";  292                         }  293                     }else if($this->pageNow > $this->totalPage){  294               295                         die("超出页码范围");  296                     }  297                 }  298   299                 $this->pageShow .= "<a id=\"flo_page\" class=\"pagenum\" href=\"".$this->url."p=".($this->pageNow+1)."\">".$this->nextFonts."</a>";  300             }else if($this->pageNow > $this->totalPage){  301               302                 die("超出页码范围");  303             }else{ //当前页等于总页数  304               305                 if($this->page_act != 1){  306   307                     $this->pageShow .= "<a id=\"flo_page\" class=\"pagenum\" href=\"".$this->url."p=".$this->totalPage."\">".$this->totalPage."</a>";  308                 }  309             }  310         }  311     }  312   313     /********************其它页面信息***********************/  314     public function getOtherInfo(){  315       316         //ajax方式不显示手动跳转信息  317         if($this->page_act != 1){  318   319             $this->pageShow .= "&nbsp;跳转至 ";  320   321             //跳转类型  322             if($this->skipStyle =="" ){ //不加跳转  323           324                 $this->pageShow .= "";  325             }else if($this->skipStyle == 1){ //输入框  326           327                 $this->pageShow .= "<input id=\"skip\" type=\"text\" value=\"".$this->pageNow."\">";  328           329                 $this->pageShow .= "<button id=\"go\">GO</button>";  330             }else if($this->skipStyle == 2){ //下拉菜单  331           332                 //选择下拉菜单自动跳转  333                 $this->pageShow .= "<select id=\"select_page\" onchange=\"location.href=this.options[this.selectedIndex].value;\" >";  334               335                 for($i=1;$i<=$this->totalPage;$i++){  336               337                     $this->pageShow .= "<option value=\"".$this->url."p=".$i."\"";    338                   339                     //下拉菜单默认显示当前页  340                     if($i == $this->pageNow){  341                   342                         $this->pageShow .= " selected";  343                     }  344                   345                     $this->pageShow .= ">".$i."</option>";  346                 }  347               348                 $this->pageShow .= "</select>";  349             }  350         }  351   352         $this->pageShow .= "&nbsp;&nbsp;当前第".$this->pageNow."页";  353         $this->pageShow .= "/共".$this->totalPage."页";  354         $this->pageShow .= "&nbsp;共".$this->totalNum."条";  355     }  356   357      358     /*****************获取每页第一条信息*****************/  359     public function getFirstRow(){  360       361         $firstRow = $this->perpageNum * ($this->pageNow-1) + 1;//当前页第一条是总条数中第几条  362   363         return $firstRow;  364     }  365       366     /********************获得总页数***********************/  367     public function getTotalPage(){  368       369         $totalPage = ceil($this->totalNum / $this->perpageNum);  370         return $totalPage;  371     }  372   373     /* ****************获取上一页、下一页文字*************** */  374   375     public function getPreFonts($preFonts){  376       377             return ($preFonts=="")?$this->preFonts:$preFonts;  378     }  379       380     public function getNextFonts($nextFonts){  381       382             return ($nextFonts=="")?$this->nextFonts:$nextFonts;  383     }   384   385 }
#page a.pagenum{

  margin-left:3px;
  margin-right:3px;
  padding:1px 7px;
  border:1px solid #ccc;
}

#page a.pagenum:hover{

  background-color:#95071b;
  color:#fff;
}

.selected{

  background-color:#95071b;
  color:#fff;
}

View Code

init.inc.php:

 

<?php
/**
  file:init.inc.php Smarty对象的实例化及初始化文件
*/

/* *********************Smarty设置*********************** */
//根目录路径方式,用于Smarty设置
define("ROOT",str_replace("\\","/",dirname(__FILE__))."/");

require ROOT.'libs/Smarty.class.php';
$smarty = new Smarty();

//Smarty3设置默认路径
$smarty ->setTemplateDir(ROOT.'templates/')
    ->setCompileDir(ROOT.'templates_c/')
    ->setPluginsDir(ROOT.'plugins/')
    ->setCacheDir(ROOT.'cache/')
    ->setConfigDir(ROOT.'configs');

$smarty->caching = false;
$smarty->cache_lifetime = 60*60*24; //模版缓存有效时间为1天
$smarty->left_delimiter = '<{';
$smarty->right_delimiter = '}>';

/***********************************************************/

//根目录url方式
$PHP_SELF=$_SERVER['PHP_SELF'];
$ROOT_URL='http://'.$_SERVER['HTTP_HOST'].substr($PHP_SELF,0,strrpos($PHP_SELF,'/')+1);
define(ROOT_URL,$ROOT_URL);

//模版目录url方式
define("Template_Dir",$ROOT_URL.'templates');

demo.php:

代码下载地址:

美高梅开户网址 57美高梅开户网址 58

您大概感兴趣的小说:

  • php jquery
    达成新闻标签分类与无刷新分页
  • FCKEditor+jQuery+PHP落成分页代码
  • 采用PHP+JQuery+Ajax分页的达成
  • ThinkPHP分页实例
  • 仿dedecms下拉分页样式修改的thinkphp分页类实例
  • ThinkPHP有变量的where条件分页实例
  • PHP分页类集锦
  • php 使用array函数完结分页
  • php轻巧分页类完毕情势
  • PHP读取txt文本文件并分页展现的章程
  • 分享php分页的效用模块
  • PHP完成轻易数字分页效果
  • 分享PHP函数完毕数字与文字分页代码
  • PHP完结仿Google分页效果的分页函数
  • jQuery+Ajax+PHP+Mysql达成分页呈现数据实例批注
  • PHP+jQuery+Ajax完成分页效果
    jPaginate插件的利用
 1 <?php   2    3 require 'init.inc.php';   4 require 'page.class.php';   5 require 'conn/conn.php';   6    7 $perpageNum = 10; //每页数据条数   8 $perPage = 4; //前分页偏移量   9 $floPage = 4; //后分页偏移量  10 $preFonts = ""; //"前一页"文字内容  11 $nextFonts = ""; //"下一页"文字内容  12 $page_n = 1; //样式2下是否加"前n页"、后n页,0为不加,1为加  13 $skipStyle = 1; //跳转类型,可选1、2  14 $pageStyle = 2; //样式类型,可选1、2、3( 样式3只包含"上一页"、"下一页"和页码 )  15 $page_act = 0; //0:url 和 1:ajax  16   17 if($page_act == 1){  18   19     //ajax方式分页时强制使用第二种样式  20     $pageStyle = 2;  21 }  22   23   24 $p = isset($_GET['p'])?$_GET['p']:1; //当前页码  25   26 //在page.class.php中定义__toString方法,把对象$mypage解析成字符串输出  27   28 //参数分别是:总条数、每页条数、前偏移量、"上一页"文字内容(默认为""时显示"上一页")、后偏移量、"下一页"文字内容(默认为""时显示"下一页")、当前地址栏页码数、手动跳转样式、页码显示样式、样式2是否加前n页后n页、分页方式(url/ajax)  29   30 //获得总条数  31 //输出列表  32 $sql_all = "select title from ips_archives";  33   34 //总条数  35 $totalNum = $conne->getRowsNum($sql_all);  36   37 //实例化  38 $mypageurl = new MyPageUrl($totalNum,$perpageNum,$perPage,$preFonts,$floPage,$nextFonts,$p,$skipStyle,$pageStyle,$page_n,$page_act);  39   40   41 //每页第一条  42 $firstRow = $mypageurl->getFirstRow();  43   44 //总条数  45 $totalPage = $mypageurl->getTotalPage();  46   47 //输出列表  48 $sql = "select title from ips_archives order by pubdate desc limit ".($firstRow-1).",".$perpageNum;  49   50 //取出数据(二维数组)  51 $rowsArray = $conne->getRowsArray($sql);  52   53 //显示页码  54 $pageShow = $mypageurl->preOffset($preFonts).$mypageurl->floOffset($nextFonts).$mypageurl->getOtherInfo();  55   56 $smarty->assign("Template_Dir",Template_Dir);  57 $smarty->assign("page_act",$page_act); //传递分页方式  58 $smarty->assign("pageNow",$p); //传递当前页  59 $smarty->assign("perpageNum",$perpageNum); //传递每页几条数据  60 $smarty->assign("totalPage",$totalPage); //传递总页数  61 $smarty->assign("preFonts",$mypageurl->getPreFonts($preFonts)); //传递上一页文字信息  62 $smarty->assign("rowsArray",$rowsArray);  63 $smarty->assign("mypage",$mypageurl);  64 $smarty->display("demo.html");

View Code

动用方法在demo.php的笺注里 

 

demo.html:

美高梅开户网址 59美高梅开户网址 60

 1 <!DOCTYPE html>   2 <html>   3 <head>   4 <meta charset="utf-8">   5 <title>PHP分页类</title>   6 <link href="<{$Template_Dir}>/css/common.css"  rel="stylesheet" type="text/css">   7 <link href="<{$Template_Dir}>/css/style1.css"  rel="stylesheet" type="text/css">   8 <script id="jq" src="<{$Template_Dir}>/js/jquery-1.8.3.min.js"></script>   9 </head>  10 <body>  11   12     <div id="list">  13           14         <ul id="newsul">  15               16             <{foreach $rowsArray as $val}>  17                 <li><{$val['title']}></li>  18             <{/foreach}>  19   20         </ul>  21   22     </div>  23     <div id="page"><{$mypage}></div>  24     <input id="pageNow" type="hidden" value="<{$pageNow}>">  25     <!--分页方式-->  26     <input id="page_act" type="hidden" value="<{$page_act}>">  27     <!--每页几条数据-->  28     <input id="perpageNum" type="hidden" value="<{$perpageNum}>">  29     <!--总页数-->  30     <input id="totalPage" type="hidden" value="<{$totalPage}>">  31     <!--//把smarty的变量传递给外部js-->  32     <input id="Template_Dir" type="hidden" value="<{$Template_Dir}>">  33     <input id="preFonts" type="hidden" value="<{$preFonts}>">  34 </body>  35 <script>  36   37     $(function(){  38       39         //遍历a  40         $(".pagenum").each(function(){  41           42             if($(this).text() == $("#pageNow").val()){  43           44                 $(this).addClass("selected");  45             }  46         });  47           48         //如果存在跳转输入框  49         if($("#skip").length>0){  50           51             $("#skip").keydown(function(){  52               53                 if(event.keyCode == 13){ //回车  54                   55                     self.location="demo.php?p="+$(this).val();  56                 }  57             });  58         }  59   60         //点击"GO"按钮跳转  61         if($("#go").length>0){  62           63             $("#go").click(function(){  64               65                 self.location="demo.php?p="+$("#skip").val();  66             });  67         }  68           69         //如果分页方式是ajax,则加载外部ajax.js  70         if($("#page_act").val() == 1){  71           72             //把smarty的变量传递给外部js  73             $Template_Dir = $("#Template_Dir").val();  74             $preFonts = $("#preFonts").val();  75   76             $insertAjax = $("<script src=\"<{$Template_Dir}>/js/ajax.js\"><\/script>");  77             $insertAjax.insertAfter($("#jq"));  78         }  79   80         //最后一行row去掉border-bottom  81         $("#list ul").children("li:last").css("border-bottom",0);  82     });  83 </script>  84 </html>

View Code

 

ajaxpage.php:

美高梅开户网址 61美高梅开户网址 62

 1 <?php   2    3 require 'conn/conn.php';   4    5 if(isset($_POST['pageNow']) && !empty($_POST['pageNow'])){   6    7     $pageNow = $_POST['pageNow'];   8 }   9   10 //每页几条数据  11 if(isset($_POST['perpageNum']) && !empty($_POST['perpageNum'])){  12   13     $perpageNum = $_POST['perpageNum'];  14 }  15   16 //当前页第一条数据  17 $firstRow = $perpageNum * ($pageNow-1) + 1;  18   19 $sql = "select title from ips_archives order by pubdate desc limit ".($firstRow-1).",".$perpageNum;  20   21   22 $rowsArray = $conne->getRowsArray($sql);  23   24 //把二维数组转换成json格式  25 echo json_encode($rowsArray);

View Code

 

ajax.js:

美高梅开户网址 63美高梅开户网址 64

  1 //删除原先的li,插入gif    2 function ajaxpre(){    3     4     //删除原先的title    5     $("#newsul li").remove();    6     7     //插入gif图    8     $loading = $("<img class=\"loading\" src=\""+$Template_Dir+"/images/loading.gif\">");    9    10     $loading.appendTo($("#newsul"));   11 }   12    13 //隐藏翻页信息   14 function infoAct(){   15    16     //当前页到达尾页时,"下一页"和"末页"   17     if(parseInt($("#pageNow").val()) == parseInt($("#totalPage").val())){   18            19         $("#flo_page").hide();   20         $("#last_page").hide();   21            22         $("#pre_page").show();   23         $("#first_page").show();   24            25     }else if(parseInt($("#pageNow").val()) == 1){ //当前页到达时隐藏"首页"和"上一页"   26        27         $("#pre_page").hide();   28         $("#first_page").hide();   29            30         $("#flo_page").show();   31         $("#last_page").show();   32     }else{   33        34         if($("#pre_page").is(":hidden") || $("#pre_page").length == 0){   35             $("#pre_page").show();   36         }   37         if($("#first_page").is(":hidden") 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图