PHP從練項目之數字分頁效果

來源:互聯網
上載者:User
PHP自練項目之數字分頁效果

學習要點:
1.LIMIT 用法
2.各種參數
3.超連結調用

第一:先在檔案中設定數字分頁模組;我的檔案是(blog.php)

//分頁模組$_page = $_GET['page'];$_pagesize = 10;$_pagenum = ($_page - 1) * $_pagesize;//首頁要得到所有的資料總和$_num=mysql_num_rows(_query("SELECT tg_id FROM tg_user"));$_pageabsolute=$_num / $_pagesize;

要注意的是在資料庫中取集的時候

//我們必須每次重新讀取結果集,而不是從新去執行SQL語句。
$_result = _query("SELECT tg_username,tg_sex,tg_face FROM tg_user ORDER BY tg_reg_time DESC LIMIT $_pagenum,$_pagesize");
設定分頁迴圈的效果

    
 
    for($i=0;$i<$_pageabsolute;$i++){ if ($_page == ($i+1)) { echo '
  • $i+1).'" class="selected">'.($i+1).'
  • '; }else{ echo '
  • $i+1).'">'.($i+1).'
  • '; } } ?>

相對應的CSS

#page_num {height:20px;clear:both;padding:10px 0;position:relative;}#page_num ul {position:absolute;right:30px;height:20px;}#page_num ul li {float:left;width:26px;height:20px;}#page_num ul li a {display:block;width:20px;height:20px;line-height:20px;border:1px solid #333;text-align:center;text-decoration:none;}#page_num ul li a:hover,#page_num ul li a.selected {background:#666;font-weight:bold;color:#fff;}

  

在其中可能會由於編碼出現容錯誤,解決的方法是

// 分頁模組if (isset ( $_GET ['page'] )) {    // 在資料不再資料範圍內出錯的解決方案    $_page = $_GET['page'];    // 是否為空白,是否小於0,是否不是數字。//如果其中有一個是,那麼就=1    if (empty ( $_page )||$_page < 0 || !is_numeric( $_page )) {        $_page = 1;    } else {        $_page = intval ( $_page ); // 如果是數字,但是小數,那麼就$_page = intval($_page);轉換成整數    }} else {    $_page = 1;}$_pagesize = 10;$_num = _num_rows( _query ( "SELECT tg_id FROM tg_user" ) );if ($_num==0) {    $_pageabsolute=1;}else{    $_pageabsolute=ceil($_num/$_pagesize);}//當頁碼大於總頁碼的時候,就會返回到總頁碼的最後一頁if ($_page>$_pageabsolute) {    $_page=$_pageabsolute;}$_pagenum = ($_page - 1) * $_pagesize;

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.