以 php 为单位进行分页.AJAX,PHP,MYSQL


Pagination in php. AJAX,PHP, MYSQL

我正在创建一个论坛类型的网站,在那里我使用 ajax 动态显示帖子。当用户登录时,他会找到一个"排序"下拉选择选项,他可以在其中选择帖子的顺序。


选择菜单

<select name="orderby" id="orderby" onchange="showposts(this.value)" >
<option value="1" selected>By Time</option>
<option value="2">By Genuine Count</option>
<option value="3">By Dubious Count</option>
</select>

当页面加载时,调用 window.onload 函数,该函数调用 'showposts()' 函数来显示帖子。


onload()

window.onload=function(){
showposts();
};

showposts() 函数

function showposts(str){
    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    } else { 

        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(str != undefined){
        currentType=str; //save the current type for later use
        document.getElementById("postsdiv").innerHTML = "";
    }else{
        var e = document.getElementById("orderby");
         str = e.options[e.selectedIndex].value;
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
           // alert(xmlhttp.responseText);
            document.getElementById("postsdiv").innerHTML=xmlhttp.responseText;
        }
    };
    xmlhttp.open("GET","showposts.php?q="+str,true);
    xmlhttp.send();
}

showposts 的一部分.php如果所选选项为 1,则从数据库中获取帖子的页面

if(intval($_GET['q'])==1){

while($row = $result->fetch_assoc()) {
    echo "<div class='postclass'>";
    echo "<span id='postspan".$row['id']."' name='postspan".$row['id']."' >";
    echo "<span id='editspan".$row['id']."' name='editspan".$row['id']."' >";
    echo "</br>";
    echo "Posted By: &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp <span class='bold'> ".$row['user']."</span>";
    if($username==$row['user']){
        echo "<a href='javascript:void(0);' onclick='deletepost(".$row['id'].")' >DELETE </a>&nbsp&nbsp&nbsp";
        echo "<a href='javascript:void(0)'onclick='editpost(".$row['id'].",'"".$row['subject']."'",'"".$row['post']."'")' >EDIT </a></br>";
    }else{
        echo "</br>";
}
    echo "<a id=".$row['id']."></a>";
    echo "Date & Time: ".$row['date']."</br>";
    echo "<span id=genuinecount".$row['id'].">Genuine Count: ".$row['genuine']."</span></br>";
    echo "<span id=dubiouscount".$row['id'].">Dubious Count: ".$row['dubious']."</span>";
    echo "</br>------------------------ </br>";
    echo "Subject:  <span class='bold' >".$row['subject']."</span></br>";
    echo "Post: ";
    echo "<div class='postbox' >&nbsp&bull;&nbsp".$row['post'] . "</div><br /></br>";
}
}

那么,我的问题是如何为此脚本添加分页?谁能帮忙?


查询

$sql = "SELECT * FROM posts order by date desc";
$result = $connection->query($sql);

showposts() 函数

    function showposts(str, page, pagesize){
        if (window.XMLHttpRequest) {
            xmlhttp=new XMLHttpRequest();
        } else { 

            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        if(str != undefined){
            currentType=str; //save the current type for later use
            document.getElementById("postsdiv").innerHTML = "";
        }else{
            var e = document.getElementById("orderby");
             str = e.options[e.selectedIndex].value;
        }
        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
               // alert(xmlhttp.responseText);
                document.getElementById("postsdiv").innerHTML=xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET","showposts.php?q="+str+'&page='+page+'&pagesize='+pagesize,true);
        xmlhttp.send();
    }

查询

$page = intval($_GET['page');
$pagesize = intval($_GET['pagesize']);
$sql = "SELECT * FROM posts order by date desc limit " 
. ($page-1)*$pagesize . ", " . $pagesize;

示例链接

<a href="#" onclick="showposts(document.getElementById('orderby').value,1,20);">Page 1, 20 per page</a>