php+ajax分頁代碼

來源:互聯網
上載者:User

<html>
<head>
<script language="javascript">
function createXMLHttp(){
    if(window.ActiveXObject){
        return new ActiveXObject("Microsoft.XMLHttp");
    }
    else if(window.XMLHttpRequest){
        return new XMLHttpRequest();
    }
}

function Pager(){
    var that=this;

    this.link=function(url){
        that.xmlHttp = createXMLHttp();
        that.xmlHttp.onreadystatechange = that.receive;
        that.xmlHttp.open("GET", url, true);
        that.xmlHttp.send(null);
    }

    this.receive=function(){
        if((that.xmlHttp.readyState == 4)){
            if(that.xmlHttp.status == 200){
                that.reaction(that.xmlHttp.responseXML);
            }else{
                that.recover();
            }
        }
    }
}
</script>
</head>
<body>
<div id='page_content'></div>
<div id='page_bar'></div>
<script language="javascript">
var a = new Pager();
var pages = 0;

a.recover = function (){
    go = function (u){
        go = function (){
        };
        a.link(u);
    };
};

a.reaction = function (xml){
    document.getElementById('page_content').innerHTML = xml.getElementsByTagName('content')[0].childNodes[0].nodeValue;

    if(xml.getElementsByTagName('count')[0].childNodes[0].nodeValue != pages){
        s = '<table><tr>';
        for(i = 1; i <= xml.getElementsByTagName('count')[0].childNodes[0].nodeValue; i++)
        {
            if(i == xml.getElementsByTagName('current')[0].childNodes[0].nodeValue)
                s += '<td><b>' + i + '</b></td>';
            else
                s += '<td><a href="javascript:go(\'page.php?page=' + i + '\')">' + i + '</a></td>';
        }
        s += '</tr></table>';
   
        document.getElementById('page_bar').innerHTML = s;
    }

    a.recover();
}


a.link('page.php');
</script>
</body>
</html>

 page.php代碼.

<?php
class class_page
{
    private $record_count, $perpage;
    private $page_count, $page_current;
   
    function __construct($perpage, $record_count, $page_current)
    {
        $this->perpage = $perpage;
        $this->record_count = $record_count;
        $this->page_count = ($record_count == 0) ? 1 : ceil($record_count / $perpage);
        $this->page_current = ($page_current > $this->page_count) ? 1 : $page_current;
    }

    function __get($name)
    {
        switch($name)
        {
        case 'page_count':
            return $this->page_count;
        case 'page_current':
            return $this->page_current;
        case 'record_start':
            return ($this->page_current - 1) * $this->perpage;
        }
    }
}

header('Content-Type: text/xml; charset=gbk');
$page = new class_page(20, 150, is_numeric($_GET['page']) ? $_GET['page'] : 1);
echo '<?xml version="1.0"?>';
echo '<page>';
echo '<count>' . $page->page_count . '</count>';
echo '<current>' . $page->page_current . '</current>';
echo '<content>' . pow($page->page_current, 2) . '</content>';
echo '</page>';
?>

相關文章

聯繫我們

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