-
-
- @session_start();
- require_once("smarty.php");//載入smarty模板類別
- require_once("adodb/adodb.inc.php");//載入adodb資料庫
//連線資料庫
- $dbhost = 'localhost'; // 資料庫伺服器
- $dbuser = 'root'; // 資料庫使用者名稱
- $dbpwd= 'root'; // 資料庫密碼
- $dbname='hejia'; //資料庫名稱
$conn = NewAdoConnection('mysql'); // 建立連線物件
- $conn->Connect($dbhost, $dbuser, $dbpwd, $dbname); //連接資料庫
- $conn->Query ("Set Names 'gb2312'");//源自mysql_query("SET NAMES GBK");
- date_default_timezone_set(PRC); //PHP取得時間差8小時的解決方案,也可在php.ini裡修改date.timezone為PRC
- ?>
-
複製程式碼
首頁index.php:
-
-
- require_once("const.php");
- $sql1="select id,hits, title,add_date from oa_art order by id desc"; //SQL語句
- $pageSize=4; //設定每頁記錄數
- $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位址
- $totalnumber=count($news_array1); //取得總記錄數
- $midPage=5; //數位導覽連結數
- page();// 呼叫分頁函數
for ($i = 0; $i $smarty->assign("page_option",$page_option);
$smarty->assign("news_data",$news_array);
- $smarty->assign("mytitle","企業網站首頁");
- $smarty->display("tpl.htm",$ page);
- ?>
-
複製程式碼
分頁函數:
-
-
function page()
- {
- global $spagey,$start ,$pageSize,$midPage,$totalnumber;
- $total = $totalnumber ; //取得總記錄數
- $totalPage = ceil($total/$pageSize); //取得總頁數
- $
- $
- $ if(!is_numeric($currentPage) || $currentPage $totalPage)
- $currentPage=1; //初始化當前頁
- $url = preg_replace(array("!(([&]|^)(page)[=]?([^&]+)?)|( (([&]){2,})|(^[&])|([&]$))!",),array(""),$_SERVER["QUERY_STRING"]); // 設定位址,正規替換掉
- $url.=($url?"&":"").'page'; // 追加
- $start = ($currentPage-1)*$pageSize;
- $ back = $currentPage > 1?"n ":"";
- $next = $currentPage > >n":"";
- $first = $currentPage > 1?"首頁n":"";
- $last = $currentPage 尾頁n":" ";
// 導覽連結
- $midPages = '';
- $num = $currentPage-floor($midPage/2);
- if($num > 0)
- {
- if(($totalPage-$num) {
- $tmp = $totalPage - $midPage;
- $num = $tmp }
- }else $num = 1;
- for($i=1; $i {
- if($num > $totalPage) break;
- $midPages .= ($num == $currentPage) ? '['.$num.'] ' : "".$num." ";
- }
- $smarty->assign("page_total",$total); / / 總計
- $smarty->assign("page_currentPage",$currentPage); // 當前頁碼
- $smarty->assign("page_totalPage",$totalPage); // 總頁數
- $smarty ->assign("page_back",$back); // 上一頁
- $smarty->assign("page_next",$next); // 下一頁
- $smarty->assign("page_first ",$first); // 首頁
- $smarty->assign("page_last",$last); // 尾頁
- $smarty->assign("page_midPages",$midPages); // 中間頁
- $smarty->assign("page_url",$page_url); // 目前頁位址
- }
?>
複製程式碼範本頁 tpl.htm:
|