Ajax PHP文章分页器

Ajax PHP文章分页器,php,ajax,paginator,Php,Ajax,Paginator,我正试图找到通过Ajax和Php为一篇长文章分页的代码,例如 我的帖子有2000多个单词,我想把这个页面分成几个页面,每个页面有500个单词。因此,总共应该显示4个链接 到目前为止,我得到的是对数据库记录进行分页的插件 如果您想检查一篇文章中有多少个字符,您可以使用PHP strlen函数。然后,如果它大于500,您可以使用$\u GET设置一个新链接。。。我不知道您是否有数据库,但我建议将文章保存在mysql数据库中。 代码: 因为我不知道你到底想要什么看这段视频:问我们推荐或查找一本书、工具

我正试图找到通过Ajax和Php为一篇长文章分页的代码,例如 我的帖子有2000多个单词,我想把这个页面分成几个页面,每个页面有500个单词。因此,总共应该显示4个链接

到目前为止,我得到的是对数据库记录进行分页的插件


如果您想检查一篇文章中有多少个字符,您可以使用PHP strlen函数。然后,如果它大于500,您可以使用$\u GET设置一个新链接。。。我不知道您是否有数据库,但我建议将文章保存在mysql数据库中。 代码:


因为我不知道你到底想要什么看这段视频:

问我们推荐或查找一本书、工具、软件库、教程或其他非现场资源的问题都会导致堆栈溢出,因为它们往往会吸引自以为是的答案和垃圾邮件。相反,请描述问题以及迄今为止为解决该问题所做的工作。简言之->ajax post到php文件,php文件将获取分割数据,以便您为查询传递限制/偏移量变量,并将结果返回到页面上。。在php页面中,您将创建prev/next/total pages按钮的结构,因为您将获得所有可用信息Hello Mark Frankli,非常感谢您的回复。然而,正如我所指出的,您给我的是一个用于数据库记录的Ajax分页器。我需要的是一篇文章的ajax分页器,例如Wordpress文章有一个长博客文章的“下一篇”和“上一篇”按钮。如果你只有一篇很长的文章,那么你可以简单地使用一个自动功能,每当你到达页面底部时都会加载内容。我不建议将一篇文章分成多个部分,但是如果您真的想这样做,您可以通过删除和重写数据库交互部分来轻松地使用上面的代码。
// This first query is just to get the total count of rows
$sql = "SELECT COUNT(id) FROM articles WHERE account_name=?"; // or you can use an strlen function here
$stmt = $conn->prepare($sql);
$stmt->bind_param("s",$u);
$stmt->execute();
$stmt->bind_result($rows);
$stmt->fetch();
$stmt->close();
// Here we have the total row count
// This is the number of results we want displayed per page
$page_rows = 10;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
    $last = 1;
}
// Establish the $pagenum variable
$pagenum = 1;
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) { 
    $pagenum = 1; 
} else if ($pagenum > $last) { 
    $pagenum = $last; 
}
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
    /* First we check if we are on page one. If we are then we don't need a link to 
       the previous page or the first page so we do nothing. If we aren't then we
       generate links to the first page, and to the previous page. */
    if ($pagenum > 1) {
        $previous = $pagenum - 1;
        $paginationCtrls .= '<a href="user.php?u='.$u.'&pn='.$previous.'#posts">Previous</a> &nbsp; &nbsp; '; // here we set up the link
        // Render clickable number links that should appear on the left of the target page number
        for($i = $pagenum-4; $i < $pagenum; $i++){
            if($i > 0){
                $paginationCtrls .= '<a href="user.php?u='.$u.'&pn='.$i.'#posts">'.$i.'</a> &nbsp; ';
            }
        }
    }
    // Render the target page number, but without it being a link
    $paginationCtrls .= ''.$pagenum.' &nbsp; ';
    // Render clickable number links that should appear on the right of the target page number
    for($i = $pagenum+1; $i <= $last; $i++){
        $paginationCtrls .= '<a href="user.php?u='.$u.'&pn='.$i.'#posts">'.$i.'</a> &nbsp; ';
        if($i >= $pagenum+4){
            break;
        }
    }
    // This does the same as above, only checking if we are on the last page, and then generating the "Next"
    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationCtrls .= ' &nbsp; &nbsp; <a href="user.php?u='.$u.'&pn='.$next.'#posts">Next</a> ';
    }
}