程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php如何實現分頁功能

php如何實現分頁功能

編輯:PHP綜合

上星期工作中,遇到要處理一段分頁的代碼!以前是寫的很熟練的,可能是懶散的太久了!感覺忘的差不多了。

有的知識就是太久不用也會忘記的很快啊!所以經常復習一下以前的知識也是很重要的。所以特地整理出一下分頁代碼!

效果如上圖;在發表框點一下彈出一個框,首先判斷用戶是否登錄,沒有登錄就彈出登錄框,已登錄就彈出輸入框,輸入留言內容,姓名和手機號。

當你輸入好內容後會在下面顯示你的留言內容,前三個為用戶最新的留言排序,從第四個起按點贊的數量進行排序。

<!--視圖層-->    
<?php    
$pageIndex = isset($_GET['p'])?$_GET['p']:0;    
echo "<script type=\"text/javascript\">var pageindex=".$pageIndex." </script>";    
?>    
<script type="text/javascript" src="/jquery.js"></script>    
<script type="text/javascript">    
function loadMore(){           
        pageindex = pageindex || 0;    
        $.ajax({    
            url:'http://www.*****.com/****/wishingwall?pageindex='+pageindex+'&callback=?',    
            type:'GET',    
            dataType:'jsonp',    
            success:function(d){    
                if(d.code == 200){    
                    if(d.font == ''){    
                        return;    
                    }else{    
                        var _html = '';    
                        if (pageindex == 0) {    
                            var _len=0;                             
                            if(d.font.length>3){                                 
                                _len=4;    
                            }else{    
                                _len=d.font.length;    
                            }    
                            for(var j = 0; j<_len; j++){    
                                var _d = d.font;    
                                _html += '<div class="wallCenter" ><div class="items"><div class="style'+random(1, 5)+'">';    
                                _html += '<div class="content">' +_d[j].content +'</div><span class="jianjue"></span><p class="author">';    
                                _html += '<span class="zanImg" style="cursor:pointer"></span><span class="username">' +_d[j].username + '</span>';    
                                _html += '<span class="zanColor">(<span id="'+_d[j].id+'" class="praisenumer'+_d[j].id+'">' +_d[j].praisenumer +'</span>贊)</span></p></div></div></div>';                       
                            }    
                        }    
                        if(d.data != '') {    
                            var _len=0;                             
                            if(d.data.length>5 && pageindex==0){                                 
                                _len=5;    
                            }else{    
                                _len=8;    
                            }    
                            for(var j = 0; j<_len; j++){    
                                var _s = d.data;    
                                _html += '<div class="wallCenter" ><div class="items"><div class="style'+random(1, 5)+'">';    
                                _html += '<div class="content">' +_s[j].content +'</div><span class="jianjue"></span><p class="author">';    
                                _html += '<span class="zanImg" style="cursor:pointer"></span><span class="username">' +_s[j].username + '</span>';    
                                _html += '<span class="zanColor">(<span id="'+_s[j].id+'" class="praisenumer'+_s[j].id+'">' +_s[j].praisenumer +'</span>贊)</span></p></div></div></div>';                       
                            }    
                        }                                                                   
                        $(".wallWrap").text('');         <!--把留言寫入-->                   
                        $(".wallWrap").html(_html).masonry({itemSelector: '.wallCenter'});      
                        var numberthis = parseInt(d.page) + 1;    
                        thisPage(d.count, numberthis);                          
                        $(".zanImg").each(function(index, elm){    
                            var $elm = $(elm);    
                            $elm.click(function(){    
                                var thisid = $elm.parent('p').find('span.zanColor span').attr('id');    
                                $.ajax({    
                                    url:'http://www.*****.com/****/addpraisenumer/'+thisid+'?callback=?',    
                                    type:'GET',    
                                    dataType:'jsonp',    
                                    success:function(d){                                                                                
                                        if(d.code == 200){                                                  
                                            $(".praisenumer"+thisid).text(d.praisenumer);    
                                        } else {    
                                            return;    
                                        }    
                                    }    
                                });    
                            })    
                        })    
                                
                    }    
                }else{    
                    return;     
                }       
            }       
        });     
}    
function thisPage(count, curPage) {    
    count = count || 1;    
    count = Number(count); <!--定義記錄數-->                          
    curPage = curPage || 1;    
    curPage = Number(curPage);  <!--當前頁-->  
    var per = 8;              
    per = Number(per);    
    var showPages = 0;    
    showPages = Number(showPages);    
    var showEnd = 0;    
    showEnd = Number(showEnd);    
    if (count%per > 0) {        <!--定義總的頁數-->  
        var pages = (parseInt(count/per) + 1);    
    } else {    
        var pages = parseInt(count/per);    
    }    
    var str = '<span class="loadPage">'+curPage+'/'+ pages +' 頁</span>';    
    str += '<span class="totalPage"> 共 '+ pages +' 頁</span>'  
    if(curPage != 1){    
       str += '<span class="firstPage" data-page="1"><a href="http://*****.*****.com/thanksgiving?p=0#content">首頁</a></span>'  
    }    
    if(curPage - 1 > 0){    
       str += '<span class="prevPage" data-page="'+(curPage-1)+'"><a href="http://*****.*****.com/thanksgiving?p='+ (curPage-2) +'#content">上一頁</a></span>'  
    }
//URL:http://www.bianceng.cn/webkf/PHP/201410/45955.htm
    if(pages <= 5){    
     showPages = 1;    
     showEnd = pages;    
    }else if(pages - curPage >= 4){    
     showPages = curPage;    
     showEnd = curPage+4;    
    }else if(pages - curPage < 4){    
     showPages = pages - 4;    
     showEnd = pages;    
    }    
    for(var i=showPages; i<=showEnd; i++){    
     if( i == curPage){    
        str += '<a class="page_trigger page_cur" data-page="'+i+'" onclick="javascript:;">'+ i +'</a>'   
     }else{    
        str += '<a class="page_trigger"  data-page="'+i+'" href="http://*****.*****.com/thanksgiving?p='+(i-1)+'#content">'+ i +'</a>'   
     }    
    }    
    if(curPage + 1 <= pages){    
       str += '</span><span class="nextPage" data-page="'+(curPage+1)+'"><a href="http://*****.*****.com/thanksgiving?p='+ curPage +'#content">下一頁</a></span>';    
    }    
    if(curPage!=pages){    
       str += '<span class="lastPage" data-page="'+pages+'"><a href="http://*****.*****.com/thanksgiving?p='+ (pages-1) +'#content">尾頁</a></span>';    
    }           
    $('.page_box').eq(0).html(str);}    
        
</script>
<!--控制層-->  
<?php  
    // 留言板顯示  
    public function action_wishingwall()  
    {  
        header('Content-Type: application/x-javascript');  
        $callback = isset( $_GET[ 'callback' ] ) ? $_GET[ 'callback' ] : 'callback';  
        $pageIndex = Arr::get($_GET, 'pageindex', 0);  
        $offset = 0;  
        try {  
      
            $font = DB::select('id', 'content', 'username', 'phone', 'userid', 'usernumber',  
                'praisenumer', 'modefied', 'created')  
                ->from('tmp_***')  
                ->order_by('praisenumer', 'desc')  
                ->limit(3)  
                ->fetch_all();     //根據點贊倒序查詢  
            $list = ' (';  
            foreach($font as $v) {  
                $list.= $v['id'].',';  
            }  
            $list.='0)';  
            $pageSize = 8;  
            if($pageIndex == 0) {  
                $pageSize = 8;  
            } else {  
                $offset = 8+($pageIndex-1)*8;  
            }  
                  
            $rows = DB::select('id', 'content', 'username', 'phone', 'userid', 'usernumber',  
                'praisenumer', 'modefied', 'created')  
                ->from('tmp_*****')  
                ->where('id', DB::expr('not in'), DB::expr($list))  
                ->order_by('created', 'desc')  
                ->limit($pageSize)  
                ->offset($offset)  
                ->fetch_all();   //根據時間倒序查詢  
            $count = DB::select('id')  
                ->from('tmp_*****')  
                ->count_all();  
            $data = array(  
                'code'  => 200,  
                'msg'   => 'Success',  
                'page'  => $pageIndex,  
                'count' => $count,  
                'font'  => $font,  
                'data'  => $rows,  
            );  
        } catch (Exception $e) {  
            $data = array(  
                'code'  => 3000003,  
                'msg'   => '查詢異常',  
                'data'  => $rows,  
            );  
        }  
        echo $callback.'('.json_encode($data).')';  
        exit;  
    }  
          
          
    // 點贊加  
    public function action_addpraisenumer($id)  
    {  
        header('Content-Type: application/x-javascript');  
        $callback = isset( $_GET[ 'callback' ] ) ? $_GET[ 'callback' ] : 'callback';  
        if (isset($id) && $id) {  
            // 得到系統唯一session_id  
            $session_id = session_id();  
            $result = array(1, time());  
            if (!$this->cacheFilter($session_id, "praise", $result, 10)) {  
                $data = array(  
                    'code'  => 3000004,  
                    'msg'   => '10秒之內無法重復提交',  
                );  
            } else {  
                DB::update('tmp_*****')  
                    ->set(array('praisenumer' => DB::expr(' praisenumer+1 ')))  
                    ->where('id', '=', $id)  
                    ->execute();  
                $praisenumer = DB::select('praisenumer')  
                    ->from('tmp_*****')  
                    ->where('id', '=', $id)  
                    ->fetch_one();  
                $data = array(  
                    'code'  => 200,  
                    'praisenumer' => $praisenumer,  
                    'msg'   => '成功',  
                );  
            }  
        } else {  
            $data = array(  
                'code'  => 3000003,  
                'msg'   => '非法的操作',  
            );  
        }  
        echo $callback.'('.json_encode($data).')';  
        exit;  
    }     
          
          
          
          
          
          
        // 新增留言板  
    public function action_addwishingwall()  
    {  
        header('Content-Type: application/x-javascript');  
        $callback = isset( $_GET[ 'callback' ] ) ? $_GET[ 'callback' ] : 'callback';  
        $content = Arr::get($_GET, 'content', '');  
        $username = Arr::get($_GET, 'username', '');  
        $phone = Arr::get($_GET, 'phone', '');  
        if (empty($content)) {  
            $data = array(  
                'code'  => 3000001,  
                'msg'   => '留言內容不能為空',  
            );  
        } else {  
            $data = array(  
                'content' => $content,  
                'username' => $username,  
                'phone' => $phone,  
                'praisenumer' => 0,  
                'modefied' => time(),  
                'created' => time()  
            );  
            try {  
                DB::insert('tmp_*****', array_keys($data))  
                    ->values(array_values($data))  
                    ->execute();  
                $data = array(  
                    'code'  => 200,  
                    'msg'   => '成功',  
                );    
            } catch (Exception $e) {  
                $data = array(  
                    'code'  => 3000002,  
                    'msg'   => '新增失敗',  
                );  
            }  
        }  
        echo $callback.'('.json_encode($data).')';  
        exit;  
    }  
    ?>

這一段為php的分頁代碼。臨時在本地服務器上測試的,效果圖為簡單的這樣!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved