@ Session_start ();
- Require_once ("smarty. php"); // load the smarty template class
- Require_once ("adodb/adodb. inc. php"); // load the adodb database
// Connect to the database
- $ Dbhost = 'localhost'; // database server
- $ Dbuser = 'root'; // database username
- $ Dbpwd = 'root'; // database password
- $ Dbname = 'hejia '; // database name
$ Conn = NewAdoConnection ('mysql'); // create a connection object
- $ Conn-> Connect ($ dbhost, $ dbuser, $ dbpwd, $ dbname); // Connect to the database
- $ Conn-> Query ("Set Names 'gb2312'"); // Source: mysql_query ("set names gbk ");
- 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.
- ?>
Home index. php:
Require_once ("const. php ");
- $ Sql1 = "select id, hits, title, add_date from oa_art order by id desc"; // SQL statement
- $ PageSize = 4; // set the number of records per page
- $ SQL = $ sql1. "limit". ($ pageSize * (empty ($ _ REQUEST ['Page'])? 1: $ _ REQUEST ['Page'])-1). ",". $ pageSize;
- $ News_array = $ conn-> getall ($ SQL );
- $ News_array1 = $ conn-> getall ($ sql1 );
- $ Page_url = "index. php"; // url of the page
- $ Totalnumber = count ($ news_array1); // obtain the total number of records
- $ MidPage = 5; // number of digital navigation links
- Page (); // call the paging function
For ($ I = 0; $ I <= ceil ($ totalnumber/$ pageSize); $ I ++) $ page_option [] = $ I; // generates an array of paging drop-down lists
- $ Smarty-> assign ("page_option", $ page_option );
$ Smarty-> assign ("news_data", $ news_array );
- $ Smarty-> assign ("mytitle", "Enterprise website homepage ");
- $ Smarty-> display ("tpl.htm", $ page );
- ?>
Paging functions:
Function page ()
- {
- Global $ smarty, $ start, $ page_url, $ pageSize, $ midPage, $ totalnumber;
- $ Total = $ totalnumber; // obtain the total number of records
- $ TotalPage = ceil ($ total/$ pageSize); // obtain the total number of pages
- $ CurrentPage = @ $ _ REQUEST ['Page'] + 0; // current page
- If (! Is_numeric ($ currentPage) | $ currentPage <1 | empty ($ currentPage) | $ currentPage> $ totalPage)
- $ CurrentPage = 1; // initialize the current page
- $ Url = preg_replace (array ("! ([&] | ^) (Page) [=]? ([^ &] + )?) | ([&]) {2,}) | (^ [&]) | ([&] $ ))! ",), Array (" "), $ _ SERVER [" QUERY_STRING "]); // Set the address, replace the regular expression
- $ Url. = ($ url? "&": ""). 'Page'; // Append
- $ Start = ($ currentPage-1) * $ pageSize;
- $ Back = $ currentPage> 1? "<\ N ":"";
- $ Next = $ currentPage <$ totalPage? ">\ N ":"";
- $ First = $ currentPage> 1? "Homepage\ N ":"";
- $ Last = $ currentPage <$ totalPage? "Last page\ N ":"";
// Navigation link
- $ MidPages = '';
- $ Num = $ currentPage-floor ($ midPage/2 );
- If ($ num> 0)
- {
- If ($ totalPage-$ num) <$ midPage)
- {
- $ Tmp = $ totalPage-$ midPage;
- $ Num = $ tmp <0? 1: ++ $ tmp;
- }
- } Else $ num = 1;
- For ($ I = 1; $ I <= $ midPage; $ I ++, $ num ++)
- {
- If ($ num> $ totalPage) break;
- $ MidPages. = ($ num = $ currentPage )? '['. $ Num. ']':"". $ Num ."";
- }
- $ Smarty-> assign ("page_total", $ total); // total
- $ Smarty-> assign ("page_currentPage", $ currentPage); // The current page number.
- $ Smarty-> assign ("page_totalPage", $ totalPage); // total number of pages
- $ Smarty-> assign ("page_back", $ back); // Previous Page
- $ Smarty-> assign ("page_next", $ next); // next page
- $ Smarty-> assign ("page_first", $ first); // home page
- $ Smarty-> assign ("page_last", $ last); // The last page.
- $ Smarty-> assign ("page_midPages", $ midPages); // intermediate page
- $ Smarty-> assign ("page_url", $ page_url); // address of the current page
- }
- ?>
Template page tpl.htm:
-
- <{$ Mytitle}>
-
-
-
-
-
-
- <{Section name = nloop loop = $ news_data}>
-
-
-
-
- <{Sectionelse}>
- <{/Section}>
-
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! |
|
- <{Include file = "bottom.htm"}>
|