search
HomeBackend DevelopmentPHP Tutorialthinkphp paging method and implementation code

  1. /**
  2. * The same code encapsulation of TODO basic paging, making the front-end code less
  3. * @param $m model, passed by reference
  4. * @param $where query conditions
  5. * @param int $pagesize Number of queries per page
  6. * @return ThinkPage
  7. */
  8. function getpage(&$m,$where,$pagesize=10){
  9. $m1=clone $m;//Shallow copy of a model
  10. $count = $m ->where($where)->count();//After the continuous operation, join and other operations will be reset
  11. $m=$m1;//To maintain the constant continuous operation, shallow copy A model
  12. $p=new ThinkPage($count,$pagesize);
  13. $p->lastSuffix=false;
  14. $p->setConfig('header','
  15. Total %TOTAL_ROW%Records per page%LIST_ROW%Records%NOW_PAGE%Pages/Total% TOTAL_PAGE%page
  16. ');
  17. $p->setConfig('prev','previous page');
  18. $p->setConfig('next','next page');
  19. $p->setConfig('last','last page');
  20. $p->setConfig('first','first page');
  21. $p->setConfig('theme' ,'%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
  22. $p->parameter=I('get.');
  23. $m->limit($p- >firstRow,$p->listRows);
  24. return $p;
  25. }
Copy code

getpage method can be placed in Application/Common/Common/function.php of the TP framework. This document can be specially Place some common methods that can be called anywhere (such as Controller files, View files, etc.).

2. Call the paging method

  1. $m=M('products');
  2. $p=getpage($m,$where,10);
  3. $list=$m->field(true)->where($ where)->order('id desc')->select();
  4. $this->list=$list;
  5. $this->page=$p->show();
  6. This is View code
  7.  {$page}
  • Copy code

    3. Pagination style

    1. .pagination ul {
    2. display: inline-block;
    3. margin-bottom: 0;
    4. margin-left: 0;
    5. -webkit-border-radius: 3px;
    6. -moz-border-radius: 3px ;
    7. border-radius: 3px;
    8. -webkit-box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    9. -moz-box-shadow: 0 1px 2px rgba(0,0,0,0.05) );
    10. box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    11. }
    12. .pagination ul li {
    13. display: inline;
    14. }
    15. .pagination ul li.rows {
    16. line-height: 30px ;
    17. padding-left: 5px;
    18. }
    19. .pagination ul li.rows b{color: #f00}
    20. .pagination ul li a, .pagination ul li span {
    21. float: left;
    22. padding: 4px 12px;
    23. line -height: 20px;
    24. text-decoration: none;
    25. background-color: #fff;
    26. background: url('../images/bottom_bg.png') 0px 0px;
    27. border: 1px solid #d3dbde;
    28. /* border-left-width: 0;*/
    29. margin-left: 2px;
    30. color: #08c;
    31. }
    32. .pagination ul li a:hover{
    33. color: red;
    34. background: #0088cc;
    35. }
    36. .pagination ul li.first-child a, .pagination ul li.first-child span {
    37. border-left-width: 1px;
    38. -webkit-border-bottom-left-radius: 3px;
    39. border-bottom-left-radius: 3px;
    40. -webkit-border-top-left-radius: 3px;
    41. border-top-left-radius: 3px;
    42. -moz-border-radius-bottomleft: 3px;
    43. -moz-border-radius-topleft: 3px ;
    44. }
    45. .pagination ul .disabled span, .pagination ul .disabled a, .pagination ul .disabled a:hover {
    46. color: #999;
    47. cursor: default;
    48. background-color: transparent;
    49. }
    50. .pagination ul .active a, .pagination ul .active span {
    51. color: #999;
    52. cursor: default;
    53. }
    54. .pagination ul li a:hover, .pagination ul .active a, .pagination ul .active span {
    55. background -color: #f0c040;
    56. }
    57. .pagination ul li.last-child a, .pagination ul li.last-child span {
    58. -webkit-border-top-right-radius: 3px;
    59. border-top-right- radius: 3px;
    60. -webkit-border-bottom-right-radius: 3px;
    61. border-bottom-right-radius: 3px;
    62. -moz-border-radius-topright: 3px;
    63. -moz-border-radius-bottomright : 3px;
    64. }
    65. .pagination ul li.current a{color: #f00;font-weight: bold; background: #ddd}
    Copy code


    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
    PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

    APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

    Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

    Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

    PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

    PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

    PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

    PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

    How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

    TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

    PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

    ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

    PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

    Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

    PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

    DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Hot Tools

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    SecLists

    SecLists

    SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)