smarty分頁的例子

來源:互聯網
上載者:User
  1. @session_start();

  2. require_once("smarty.php");//載入smarty模板類
  3. require_once("adodb/adodb.inc.php");//載入adodb資料庫

  4. //串連資料庫

  5. $dbhost = 'localhost'; // 資料庫伺服器
  6. $dbuser = 'root'; // 資料庫使用者名稱
  7. $dbpwd= 'root'; // 資料庫密碼
  8. $dbname='hejia'; //資料庫名稱

  9. $conn = NewAdoConnection('mysql'); // 建立連線物件

  10. $conn->Connect($dbhost, $dbuser, $dbpwd, $dbname); //串連資料庫
  11. $conn->Query("Set Names 'gb2312'");//來源於mysql_query("SET NAMES GBK");
  12. date_default_timezone_set(PRC); //PHP擷取時間差8小時的解決辦法,也可在php.ini裡修改date.timezone為PRC
  13. ?>

複製代碼

首頁 index.php:

  1. require_once("const.php");

  2. $sql1="select id,hits,title,add_date from oa_art order by id desc"; //SQL語句
  3. $pageSize=4; //設定每頁記錄數
  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地址
  8. $totalnumber=count($news_array1); //獲得總記錄數
  9. $midPage=5; //數字導航連結數
  10. page();// 調用分頁函數

  11. for ($i = 0; $i <=ceil($totalnumber/$pageSize); $i++) $page_option[] = $i;//產生分頁下拉式清單數組

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

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

  14. $smarty->assign("mytitle","企業網站首頁");
  15. $smarty->display("tpl.htm",$page);
  16. ?>

複製代碼

分頁函數:

  1. function page()

  2. {
  3. global $smarty,$start,$page_url,$pageSize,$midPage,$totalnumber;
  4. $total = $totalnumber ; //獲得總記錄數
  5. $totalPage = ceil($total/$pageSize); //獲得總頁數
  6. $currentPage=@$_REQUEST['page']+0; //當前頁
  7. if(!is_numeric($currentPage) || $currentPage < 1 || empty($currentPage) || $currentPage > $totalPage)
  8. $currentPage=1; //初始化當前頁
  9. $url = preg_replace(array("!(([&]|^)(page)[=]?([^&]+)?)|((([&]){2,})|(^[&])|([&]$))!",),array(""),$_SERVER["QUERY_STRING"]); // 設定地址,正則替換掉
  10. $url.=($url?"&":"").'page'; // 追加
  11. $start = ($currentPage-1)*$pageSize;
  12. $back = $currentPage > 1?"<<\n":"";
  13. $next = $currentPage < $totalPage ?">>\n":"";
  14. $first = $currentPage > 1?"首頁\n":"";
  15. $last = $currentPage < $totalPage?"尾頁\n":"";

  16. // 導航連結

  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); // 總計
  33. $smarty->assign("page_currentPage",$currentPage); // 當前頁碼
  34. $smarty->assign("page_totalPage",$totalPage); // 總頁數
  35. $smarty->assign("page_back",$back); // 上一頁
  36. $smarty->assign("page_next",$next); // 下一頁
  37. $smarty->assign("page_first",$first); // 首頁
  38. $smarty->assign("page_last",$last); // 尾頁
  39. $smarty->assign("page_midPages",$midPages); // 中間頁
  40. $smarty->assign("page_url",$page_url); // 當前頁地址
  41. }
  42. ?>

複製代碼

模板頁 tpl.htm:

  1. <{$mytitle}>
  2. <{section name=nloop loop=$news_data}>
  3. <{sectionelse}>
  4. <{/section}>
  5. ">
  6. 標題 發布日期 點擊次數
    .htm"><{$news_data[nloop].title}> <{$news_data[nloop].add_date|date_format:"%Y-%m-%d"}> <{$news_data[nloop].hits}>
    對不起,沒有任何新聞輸入!
  7. <{include file="bottom.htm"}>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.