Simple implementation of PHP page code

Source: Internet
Author: User
Tags vars

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Database Operation Class Code: mysqli.func.php

[PHP]View Plain copy
  1. <?php
  2. Database Connection Constants
  3. Define (' db_host ', ' localhost ');
  4. Define (' Db_user ', ' root ');
  5. Define (' db_pwd ', ');
  6. Define (' db_name ', ' guest ');
  7. Connecting to a database
  8. Function Conn ()
  9. {
  10. $conn = Mysqli_connect (Db_host, Db_user, Db_pwd, db_name);
  11. Mysqli_query ($conn, "set names UTF8");
  12. return $conn;
  13. }
  14. Get the result set
  15. function Doresult ($sql) {
  16. $result =mysqli_query (Conn (), $sql);
  17. return $result;
  18. }
  19. Result set to object collection
  20. function dolists ($result) {
  21. return Mysqli_fetch_array ($result, MYSQL_ASSOC);
  22. }
  23. function Totalnums ($sql) {
  24. $result =mysqli_query (Conn (), $sql);
  25. return $result->num_rows;
  26. }
  27. Close the database
  28. function Closedb ()
  29. {
  30. if (! Mysqli_close ()) {
  31. exit (' close exception ');
  32. }
  33. }
  34. ?>


Pagination Implementation code:

[PHP]View Plain copy
  1. <?php
  2. Include ' mysqli.func.php ';
  3. Total Record Count
  4. $sql = "Select dg_id from Tb_user";
  5. $totalnums = Totalnums ($sql);
  6. Show number of bars per page
  7. $fnum = 8;
  8. Number of pages
  9. $pagenum = ceil ($totalnums/ $fnum);
  10. Page Constants
  11. @$tmp = $_get[' page '];
  12. Prevent malicious paging
  13. if ($tmp > $pagenum)
  14. echo "<script>window.location.href= ' index.php ' </script>";
  15. Calculate Paging Start value
  16. if ($tmp = = "") {
  17. $num = 0;
  18. } Else {
  19. $num = ($tmp-1) * $fnum;
  20. }
  21. Query statements
  22. $sql = "Select Dg_id,dg_username from Tb_user ORDER by dg_id DESC LIMIT". $num.  ", $fnum";
  23. $result = Doresult ($sql);
  24. Traverse output
  25. while (!! $rows = dolists ($result)) {
  26. echo $rows [' dg_id ']. " " . $rows [' Dg_username '].  "<br>";
  27. }
  28. Page link
  29. for ($i = 0; $i < $pagenum; $i + +) {  
  30. echo "<a href=index.php?page=". ($i + 1). ">". ($i + 1).  "</a>";
  31. }
  32. ?>


Simple to implement the paging function, the database please configure the structure

Simple implementation of PHP page code

Contact Us

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

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

A Free Trial That Lets You Build Big!

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

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

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