使用和制作分和表格

上传人:工**** 文档编号:503667658 上传时间:2022-09-13 格式:DOCX 页数:11 大小:139.41KB
返回 下载 相关 举报
使用和制作分和表格_第1页
第1页 / 共11页
使用和制作分和表格_第2页
第2页 / 共11页
使用和制作分和表格_第3页
第3页 / 共11页
使用和制作分和表格_第4页
第4页 / 共11页
使用和制作分和表格_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《使用和制作分和表格》由会员分享,可在线阅读,更多相关《使用和制作分和表格(11页珍藏版)》请在金锄头文库上搜索。

1、使用PHP和jQuery制作分页和表格如果您已经下载了本站提供的baseProject项目源码,后台中列表页面均可作为示例,其中文章列表页面的功能最为丰富。如果您没有下载该源码,相关的js文件可以从获得,示例页面为页面。以下是后台文章列表页面的截图。分页和表格功能实例截图分页功能详解分页功能用于当目标数据过多时,为提高页面展示速度采用的一种手段。本文中的分页功能借用了Zend Framework中的Zend_Paginator对象,分页适配器为Zend_Paginator_Adapter_Null。该适配器也是最简单易用的一个。工作原理在PHP端,分页功能的基本参数为记录总数($countRo

2、ws)、每页显示的记录数($rowsPerPage)、当前页码($page 默认值为1),页码数量($items 指每次在页面上显示多少个页码,建议为单数)。其他参数可以通过计算得到:1. 页码合计($countPages)取不小于$countRows/$rowsPerPage的整数;2. 起始页码和结束页码的计算方式太长不写了;3. 页码列表:从起始页码到结束页码的页码组成的数组,如array(4, 5, 6, 7, 8)在HTML端,必要的参数为PHP端计算得出的数据,同时需要指定一个用于显示分页信息的html元素。然后使用jQuery根据参数动态生成html并将其插入到指定的html元素

3、中就行了。PHP示例代码详解1. public function articlesAction() 2. $pageNumber = $this-getRequest()-getParam(page, 1);/获取当前页码,如果未指定则设为13. $sortBy = $this-getRequest()-getParam(sortby);/获取sortby设置4. if (empty($sortBy) | ! preg_match(/a-z0-9_-+ (asc|desc)$/i, $sortBy) 5. /如果sortBy为空或者不符合格式要求则使用以下的排序方式6. /注意:一定要进行格式

4、检查,防止sql注入7. $sortBy = Project_Table:getFullyColumnName(article, id) . desc;8. 9. $mArticle = new Model_Article();10. /* 这部分是处理where子句的,和本文关系不大,略过 */11. $whereArray = array(12. article.article_category_id = $this-getRequest()-getParam(Project_Table:getFullyColumnName(article, article_category_id),13

5、. article.article_status_id = $this-getRequest()-getParam(Project_Table:getFullyColumnName(article, article_status_id),14. );15. $whereString = ;16. foreach ($whereArray as $key = $value) 17. if (! empty($value) 18. if ($key = article.article_category_id & $value = -1) 19. $whereString .= AND $key I

6、S NULL;20. else 21. $whereString .= AND $key = $value;22. 23. 24. 25. if (! empty($whereString) 26. $whereString = substr($whereString, strlen( AND );27. else 28. $whereString = null;29. 30. /* 以上是处理where子句的 */31. $maxGetRows = Project_Config:PAGINATOR_ITEM_COUNT_PER_PAGE; /设置每页显示的记录数量32. $articles

7、= $mArticle-getArticles($whereString, $sortBy, $maxGetRows, ($pageNumber - 1) * $maxGetRows); /从数据库中读取数据33. $countArticles = Project_Table:getLastFoundRows(); /获得符合条件的数据总数34. if (empty($articles) & ! empty($countArticles) 35. /如果没有取到任何记录,而且记录总数不为空,说明当前页码超出范围了,所以处理一下36. $articles = $mArticle-getArtic

8、les($whereString, $sortBy, $maxGetRows, null);37. $pageNumber = 1;38. 39. $pDate = Project_Datetime:getInstance();/时间处理工具,和本文无关40. foreach ($articles as $key = $article) 41. $articles$keyProject_Table:getFullyColumnName(article, modified) = $pDate-getUserTimeFromTime($articleProject_Table:getFullyCo

9、lumnName(article, modified), true);/把时间戳转换为用户的当地时间,和本文无关42. 43. $paginator = Zend_Paginator:factory($countArticles, Null);/调用Zend提供的分页适配器,参数1是总记录数,参数2是适配器的名字44. $paginator-setCurrentPageNumber($pageNumber);/告诉分页器当前页码45. $pages = $paginator-getPages();/得到了前面提到的所有参数,后面我会打印它,这样你会直观的看到其内容46. $this-view-

10、articles = $articles;47. $this-view-pages = $pages;48. /以下处理是为了页面中的下拉选单提供选项数据,和本文关系不大49. if (! $this-getRequest()-isPost() 50. $mpArticle = new Mapper_Article();51. $this-view-elementArticleStatusId = $mpArticle-getElement(article_status_id, array(Project_Mapper:OPTIONS_NULLABLE = true, Project_Mapp

11、er:OPTIONS_DEFAULT = );52. $_categories = $mArticle-getAllowAppendArticleArticleCategories(null, null, null, null, true);53. $categories = array(54. $this-view-translate(Model_Article:NO_CATEGORY) = -1,55. );56. foreach ($_categories as $key = $row) 57. $categories$rowProject_Table:getFullyColumnNam

12、e(article_category, name) = $rowProject_Table:getFullyColumnName(article_category, id);58. 59. $mpArticle = new Mapper_Article();60. $this-view-elementArticleCategories = $mpArticle-getElement(article_category_id, array(Project_Mapper:OPTIONS_IN_ARRAY = $categories, Project_Mapper:OPTIONS_NULLABLE =

13、 true);61. 62. $pages中的内容我打印了一下,这样更直观。如下。63. stdClass Object64. (65. pageCount = 366. itemCountPerPage = 1067. first = 168. current = 269. last = 370. previous = 171. next = 372. pagesInRange = Array73. (74. 1 = 175. 2 = 276. 3 = 377. )78.79. firstPageInRange = 180. lastPageInRange = 381. currentIte

14、mCount = 1082. totalItemCount = 2183. firstItemNumber = 1184. lastItemNumber = 2085. )Zend Framework的开发人员确实很认真,返回的参数非常充分,不需要任何的补充。顺便提一下以上代码中未体现的细节,当使用post方式访问该页面时会得到json数据而不是html。这个细节写在init()方法中,代码如下:1. public function init() 2. if ($this-getRequest()-isPost() 3. $this-_helper4. -contextSwitch()5. -addActionContext(articles, json) /就

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 试题/考题 > 初中试题/考题

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号