Example of smarty paging

Source: Internet
Author: User
Tags smarty template
Example of smarty paging

  1. @ Session_start ();

  2. Require_once ("smarty. php"); // load the smarty template class
  3. Require_once ("adodb/adodb. inc. php"); // load the adodb database

  4. // Connect to the database

  5. $ Dbhost = 'localhost'; // database server
  6. $ Dbuser = 'root'; // database username
  7. $ Dbpwd = 'root'; // database password
  8. $ Dbname = 'hejia '; // database name

  9. $ Conn = NewAdoConnection ('mysql'); // create a connection object

  10. $ Conn-> Connect ($ dbhost, $ dbuser, $ dbpwd, $ dbname); // Connect to the database
  11. $ Conn-> Query ("Set Names 'gb2312'"); // Source: mysql_query ("set names gbk ");
  12. Date_default_timezone_set (PRC); // PHP solution for obtaining the time difference of 8 hours. you can also change date. timezone to PRC in php. ini.
  13. ?>

Home index. php:

  1. Require_once ("const. php ");

  2. $ Sql1 = "select id, hits, title, add_date from oa_art order by id desc"; // SQL statement
  3. $ PageSize = 4; // set the number of records per page
  4. $ SQL = $ sql1. "limit". ($ pageSize * (empty ($ _ REQUEST ['Page'])? 1: $ _ REQUEST ['Page'])-1). ",". $ pageSize;
  5. $ News_array = $ conn-> getall ($ SQL );
  6. $ News_array1 = $ conn-> getall ($ sql1 );
  7. $ Page_url = "index. php"; // url of the page
  8. $ Totalnumber = count ($ news_array1); // obtain the total number of records
  9. $ MidPage = 5; // number of digital navigation links
  10. Page (); // call the paging function

  11. For ($ I = 0; $ I <= ceil ($ totalnumber/$ pageSize); $ I ++) $ page_option [] = $ I; // generates an array of paging drop-down lists

  12. $ Smarty-> assign ("page_option", $ page_option );

  13. $ Smarty-> assign ("news_data", $ news_array );

  14. $ Smarty-> assign ("mytitle", "Enterprise website homepage ");
  15. $ Smarty-> display ("tpl.htm", $ page );
  16. ?>

Paging functions:

  1. Function page ()

  2. {
  3. Global $ smarty, $ start, $ page_url, $ pageSize, $ midPage, $ totalnumber;
  4. $ Total = $ totalnumber; // obtain the total number of records
  5. $ TotalPage = ceil ($ total/$ pageSize); // obtain the total number of pages
  6. $ CurrentPage = @ $ _ REQUEST ['Page'] + 0; // current page
  7. If (! Is_numeric ($ currentPage) | $ currentPage <1 | empty ($ currentPage) | $ currentPage> $ totalPage)
  8. $ CurrentPage = 1; // initialize the current page
  9. $ Url = preg_replace (array ("! ([&] | ^) (Page) [=]? ([^ &] + )?) | ([&]) {2,}) | (^ [&]) | ([&] $ ))! ",), Array (" "), $ _ SERVER [" QUERY_STRING "]); // Set the address, replace the regular expression
  10. $ Url. = ($ url? "&": ""). 'Page'; // Append
  11. $ Start = ($ currentPage-1) * $ pageSize;
  12. $ Back = $ currentPage> 1? "<\ N ":"";
  13. $ Next = $ currentPage <$ totalPage? ">\ N ":"";
  14. $ First = $ currentPage> 1? "Homepage\ N ":"";
  15. $ Last = $ currentPage <$ totalPage? "Last page\ N ":"";

  16. // Navigation link

  17. $ MidPages = '';
  18. $ Num = $ currentPage-floor ($ midPage/2 );
  19. If ($ num> 0)
  20. {
  21. If ($ totalPage-$ num) <$ midPage)
  22. {
  23. $ Tmp = $ totalPage-$ midPage;
  24. $ Num = $ tmp <0? 1: ++ $ tmp;
  25. }
  26. } Else $ num = 1;
  27. For ($ I = 1; $ I <= $ midPage; $ I ++, $ num ++)
  28. {
  29. If ($ num> $ totalPage) break;
  30. $ MidPages. = ($ num = $ currentPage )? '['. $ Num. ']':"". $ Num ."";
  31. }
  32. $ Smarty-> assign ("page_total", $ total); // total
  33. $ Smarty-> assign ("page_currentPage", $ currentPage); // The current page number.
  34. $ Smarty-> assign ("page_totalPage", $ totalPage); // total number of pages
  35. $ Smarty-> assign ("page_back", $ back); // Previous Page
  36. $ Smarty-> assign ("page_next", $ next); // next page
  37. $ Smarty-> assign ("page_first", $ first); // home page
  38. $ Smarty-> assign ("page_last", $ last); // The last page.
  39. $ Smarty-> assign ("page_midPages", $ midPages); // intermediate page
  40. $ Smarty-> assign ("page_url", $ page_url); // address of the current page
  41. }
  42. ?>

Template page tpl.htm:

  1. <{$ Mytitle}>
  2. <{Section name = nloop loop = $ news_data}>
  3. <{Sectionelse}>
  4. <{/Section}>
  5. ">
    Title Release Date Clicks
    . Htm "> <{$ news_data [nloop]. title}> <{$ News_data [nloop]. add_date | date_format: "% Y-% m-% d"}> <{$ News_data [nloop]. hits}>
    Sorry, there is no news input!
  6. <{Include file = "bottom.htm"}>

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.