Home Backend Development PHP Tutorial PHP+smarty paging principle and paging code sharing

PHP+smarty paging principle and paging code sharing

Jul 25, 2016 am 08:52 AM

$ this->config_dir="smarty/smarty/config_file.class.php";
$this->caching=false;
$this->template_dir = "smarty/templates/";
    $this->cache_dir = "smarty/smarty_cache/";
  1. $this->left_delimiter = "{";
  2. $this->right_delimiter = "}";
  3. }
  4. }
  5. ?>

  6. Copy Code
  7. 2, adodb, connect database class:
  8. class conndb
{
var $dbtype;var $host;var $user;
var $pwd;

var $dbname;

var $debug;//false does not display debugging information, otherwise , display
var $conn;
function __construct($dbtype,$host,$user,$pwd,$dbname,$debug=false)
    {
  1. $this->dbtype = $dbtype;
  2. $this-> host = $host;
  3. $this->user = $user;
  4. $this->pwd = $pwd;
  5. $this->dbname = $dbname;
  6. $this->debug = $debug;
  7. }
  8. //Implement the database connection and return the connection object (personal understanding can be said to return the adodb object)
  9. function getconnid()
  10. {
  11. include_once('adodb5/adodb.inc.php');
  12. if($this-> dbtype == "mysql" || $this->dbtype == "mssql")
  13. {
  14. if($this->dbtype == "mysql")
  15. {
  16. $this->conn = newadoconnection(" mysql");//Create adodb object and declare the database type as mysql
  17. }
  18. else
  19. {
  20. $this->conn = newadoconnection("mssql");
  21. }
  22. $this->conn->connect( $this->host,$this->user,$this->pwd,$this->dbname);
  23. }
  24. else if($this->dbtype == "access")
  25. {
  26. $this->conn = newadoconnection("access");
  27. $this->conn->connect("driver={microsoft access driver (*.mdb)};dbq=".$this-> dbname.";uid=".$this->user.";pwd=".$this->pwd.";");
  28. }
  29. $this->conn->execute("set names utf-8");
  30. if($this->dbtype == "mysql")
  31. {
  32. $this->conn->debug = $this->debug;
  33. }
  34. return $this ->conn;
  35. }
  36. function closeconnid()//Close the connection to the database
  37. {
  38. $this->conn->disconnection();
  39. }
  40. }
  41. Copy code
  42. 3, Database operation class:
  43. class admindb
{
var $sqlstr; var $conn; var $sqltype;
var $rs;

var $array;

function execsql($sqlstr,$conn)
{
$rs = $conn->execute($sqlstr);
    $sqltype = strtolower(substr(trim($sqlstr),0,6)); //strtolower() function converts the string to lowercase.
  1. if($sqltype = "select")
  2. {
  3. $array = $rs->getrows(); // Similar to the array returned by the mysql_fetch_array function
  4. if(count($array) == 0 || $rs == false)
  5. {
  6. return false;
  7. }
  8. else
  9. {
  10. return $array;
  11. }
  12. }
  13. else if($sqltype = "update"||$sqltype = "insert"||$sqltype = "delete")
  14. {
  15. if($rs)
  16. {
  17. return true;
  18. }
  19. else
  20. {
  21. return false;
  22. }
  23. }
  24. }
  25. }
  26. Copy code
  27. 4,分页类:

    1. class seppage

    2. {
    3. var $rs;
    4. var $pagesize;
    5. var $nowpage;
    6. var $array;
    7. var $conn;
    8. var $sqlstr;
    9. function showdate($sqlstr,$conn,$pagesize,$nowpage)
    10. {
    11. if(!isset($nowpage)||$nowpage=="")
    12. {
    13. $nowpage = 1 ;
    14. }
    15. else
    16. {
    17. $this->nowpage = $nowpage;
    18. }
    19. $this->pagesize = $pagesize;
    20. $this->conn = $conn;
    21. $this->sqlstr = $sqlstr;
    22. $this->rs = $this->conn->pageexecute($this->sqlstr,$this->pagesize,$this->nowpage);
    23. //pageexecute($sql, $nrows, $page, $inputarr=false) 使用资料集的页码功能,叁数 $page 是以 1 为启使值
    24. $this->array = $this->rs->getrows();
    25. if(count($this->array) == 0 || $this->rs == false)
    26. {
    27. return false;
    28. }
    29. else
    30. {
    31. return $this->array;
    32. }
    33. }

    34. function showpage($contentname,$utits,$anothersearchstr,$class)

    35. {

    36. $allrs=$this->conn->execute($this->sqlstr);

    37. $record=count($allrs->getrows());
    38. $pagecount=ceil($record/$this->pagesize);

    39. $str.="共有".$contentname." ".$record." ".$utits." 每页显示 ".$this->pagesize." ".$utits." 第 ".$this->rs->absolutepage()." 页/共 ".$pagecount." 页";

    40. $str.="    ";

    41. if(!$this->rs->atfirstpage())

    42. {
    43. $str.="首页";
    44. }
    45. else
    46. {
    47. $str.="首页";
    48. }
    49. $str.=" ";

    50. if(!$this->rs->atfirstpage())

    51. {
    52. $str.="上一页";
    53. }
    54. else
    55. {
    56. $str.="上一页";
    57. }
    58. $str.=" ";

    59. if(!$this->rs->atlastpage())

    60. {
    61. $str.="下一页";
    62. }
    63. else
    64. {
    65. $str.="下一页";
    66. }
    67. $str.=" ";
    68. if(!$this->rs->atlastpage())

    69. {
    70. $str.="尾页";
    71. }
    72. else
    73. {
    74. $str.="尾页";
    75. }
    76. if(count($this->array)==0 || $this->rs==false)
    77. {
    78. return "";
    79. }
    80. else
    81. {
    82. return $str;
    83. }
    84. }
    85. }

    复制代码

    5. Classes for article character conversion processing:

    1. class usefun

    2. {
    3. function unhtml($text)
    4. {
    5. $content=(nl2br(htmlspecialchars($text))));//htmlspecialchars() function combines some predefined The characters are converted to HTML entities, and the nl2br() function inserts an HTML newline character (
      ) before each new line (/n) in the string.

    6. $content=str_replace("[strong]","",$content);

    7. $content=str_replace("[/strong]","$content=str_replace("[em]","",$content);
    8. $content=str_replace("[/em]","" ,$content);
    9. $content=str_replace("[u]","",$content);
    10. $content=str_replace("[/u]","",$ content);
    11. $content=str_replace("[font color=#ff0000]","",$content);
    12. $content=str_replace("[font color=#00ff00] ","",$content);
    13. $content=str_replace("[font color=#0000ff]","",$content);
    14. $content=str_replace("[font face=混体_gb2312]","",$content);
    15. $content=str_replace("[font face=宋体]","< font face=新宋体>",$content);
    16. $content=str_replace("[font face=official script]","",$content);
    17. $content=str_replace(" [/font]","",$content);
    18. //$content=str_replace(chr(32)," ",$content);
    19. $content=str_replace("[font size=1 ]","",$content);
    20. $content=str_replace("[font size=2]","",$content);
    21. $content= str_replace("[font size=3]","",$content);
    22. $content=str_replace("[font size=4]","", $content);
    23. $content=str_replace("[font size=5]","",$content);
    24. $content=str_replace("[font size=6]","< ;font size=6>",$content);

    25. $content=str_replace("[fieldset][legend]","

      ",$content) ;
    26. $content=str_replace("[/legend]","",$content);
    27. $content=str_replace("[/fieldset]","",$content) ;
    28. return $content;
    29. }
    30. }

    Copy the code

    Put all the above four classes into one class file system.class.inc.php.

    A few more files: 1. system.inc.php:

    1. session_start();

    2. include_once("smarty_inc.php");
    3. include_once("system.class.inc.php");
    4. //Database connection class Instantiation
    5. $connobj = new conndb("mysql","localhost","root","vertrigo","db_fenye",false);
    6. $conn = $connobj->getconnid();
    7. //Database operation Class instantiation
    8. $admindb = new admindb();
    9. //Paging class instantiation
    10. $seppage=new seppage();
    11. //Use common function class instantiation
    12. $usefun=new usefun();
    13. //Call smarty template
    14. $smarty=new smartyproject();
    15. function unhtml($params)
    16. {
    17. extract($params);
    18. $text=$content;
    19. global $usefun;
    20. return $usefun->unhtml($text );
    21. }

    22. $smarty->register_function("unhtml","unhtml");

    23. ?>

    Copy code

    2、执行文件,index.php:

    1. include_once("system.inc.php");
    2. $arraybbstell = $admindb->execsql("select * from tb_bookinfo",$conn);
    3. if(!$arraybbstell)
    4. {
    5. $smarty->assign("isbbstell","t");
    6. }
    7. else
    8. {
    9. $smarty->assign("isbbstell",t);
    10. $smarty->assign("arraybbstell",$arraybbstell);
    11. }
    12. $arraybbs = $seppage->showdate("select * from tb_bookinfo",$conn,1,$_get["page"]);
    13. if(!$arraybbs)
    14. {
    15. $smarty->assign("isbbs",f);
    16. }
    17. else
    18. {
    19. $smarty->assign("isbbs",t);
    20. $smarty->assign("showpage",$seppage->showpage("帖子","条","","a1"));
    21. $smarty->assign("arraybbs",$arraybbs);
    22. }
    23. $smarty->display("index.html");
    24. ?>
    复制代码

    3、解析文件,index.html:

    1. 页模
    2. {if $isbbs=="t"}
    3. {php}
    4. $i=1;
    5. {/php}
    6. {section name=bbsid loop=$arraybbs}
    7. {php}
    8. $i++;
    9. {/php}
    10. {/section}
    11. 图书名称
      图书内容
      出版日期
      图书作者
      {unhtml content=$arraybbs[bbsid].bookname}
      {$arraybbs[bbsid].bookintro}
      {$arraybbs[bbsid].booktime}
      {$arraybbs[bbsid].bookauthor}
    12.  {$showpage}
    13. {/if}
    14. {if $isbbstell=="f" && $isbbs=="f"}
    15. {ldelim}暂时没有内容{rdelim}
    16. {/if}
    17.  
复制代码

总结: 需切记smarty,adodb文件引入的相关一些路径问题。 在执行文件index.html中的css路径,图片背景路径都是相对于index.php这个执行文件的(如css/css.cs和index.php在同一级),引入方法是:

adodb的分页函数:pageexecute,absolutepage()当前页码,atfirstpage()首页页码,atlastpage()尾页页码。(个人理解) $_server['php_self']:当前执行的文件。 smarty的register_function()参考另文。



Statement of this Website
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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1277
29
C# Tutorial
1257
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO) How do you prevent SQL Injection in PHP? (Prepared statements, PDO) Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

See all articles