Smarty Examples of pagination

Source: Internet
Author: User
    1. @session_start ();

    2. Require_once ("smarty.php");//load Smarty template class
    3. Require_once ("adodb/adodb.inc.php");//Load ADODB Database

    4. Connecting to a database

    5. $dbhost = ' localhost '; Database server
    6. $dbuser = ' root '; Database user Name
    7. $dbpwd = ' root '; Database Password
    8. $dbname = ' Hejia '; Database name

    9. $conn = newadoconnection (' mysql '); Establish a Connection object

    10. $conn->connect ($dbhost, $dbuser, $dbpwd, $dbname); Connecting to a database
    11. $conn->query ("Set Names ' gb2312 '");//derived from mysql_query ("Set Names GBK");
    12. Date_default_timezone_set (PRC); PHP get a 8-hour time difference solution, can also be modified in php.ini Date.timezone for PRC
    13. ?>

Copy Code

Home index.php:

    1. !--? require_once ("const.php");

    2. $sql 1= "Select Id,hits,title,add_date from Oa_art ORDER by id DESC";//sql statement
    3. $pageSize = 4;//set number of records per page
    4. $sql = $sql 1. "Limit". ($pageSize * ($_request[' page ')? 1: $_request[' page '])-1). ",". $pageSize;
    5. $news _array= $conn->getall ($sql);
    6. $news _array1= $conn->getall ($sql 1);
    7. $page _url= "index.php";//page URL address
    8. $totalnumber =count ($news _array1);//Get the total number of records
    9. $midPage = 5;//number of navigation links
    10. page ();//Call paging function

    11. for ($i = 0; $i <=ceil ($totalnumber/$pageSize); $i + +) $page _option[] = $i;//Generate pagination drop-down list array

    12. $smarty->assign ("Page_option", $page _option);

    13. $smarty->assign ("News_data", $news _array);

    14. $smarty->assign ("MyTitle", "corporate website Home");
    15. $smarty->display ("tpl.htm", $page);
    16. ?

Copy Code

Paging functions:

  1. Function page ()

  2. {
  3. Global $smarty, $start, $page _url, $pageSize, $midPage, $totalnumber;
  4. $total = $totalnumber; Get Total Record Count
  5. $totalPage = Ceil ($total/$pageSize); Get Total 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
  10. $url. = ($url? ") & ":" "). ' Page '; Additional
  11. $start = ($currentPage-1) * $pageSize;
  12. $back = $currentPage > 1? " <<\ n ":" ";
  13. $next = $currentPage < $totalPage? " >>\ n ":" ";
  14. $first = $currentPage > 1? " home \ n ":";
  15. $last = $currentPage < $totalPage? " last \ n ":";

  16. Navigation links

  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); Current page number
  34. $smarty->assign ("Page_totalpage", $totalPage); Total pages
  35. $smarty->assign ("Page_back", $back); Previous page
  36. $smarty->assign ("Page_next", $next); Next page
  37. $smarty->assign ("Page_first", $first); Home
  38. $smarty->assign ("Page_last", $last); Last
  39. $smarty->assign ("Page_midpages", $midPages); Middle Page
  40. $smarty->assign ("Page_url", $page _url); Current page address
  41. }
  42. ?>

Copy Code

Template page tpl.htm:

  1. <{$mytitle}>
  2. <{section name=nloop loop= $news _data}>
  3. <{sectionelse}>
  4. <{/section}>
  5. ">
    Title Release date Click Count
    . 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"}>
Copy 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.