当用户单击“显示更多”按钮时显示更多帖子


Showing more posts when a user clicks the show more button

当我网站上的用户访问他们的个人资料页面时,他们会看到他们发布的帖子列表。我目前默认显示 20 个帖子,但无法显示较旧的帖子。我想添加一个显示更多帖子按钮,该按钮会将其他/较旧的帖子加载到页面上。在用户单击"显示更多"按钮后,我应该怎么做才能向用户显示更多帖子?

这是我的显示帖子功能

function show_posts1($userid){
$posts = array();
$sql = "SELECT p.body, p.stamp, p.id,u.username, u.imagelocation 
    FROM posts p 
    INNER JOIN users u 
    ON p.user_id=u.id 
    WHERE p.user_id='$userid' 
    ORDER BY p.stamp DESC";
$result = mysql_query($sql);
while($data = mysql_fetch_object($result)){
    $posts[] = array(   'stamp' => $data->stamp, 
                        'userid' => $userid, 
                        'body' => $data->body,
                        'username' => $data->username,
                        'imagelocation'=>$data->imagelocation,
                        'id'=>$data->id
                );
}
return $posts;
}

这是我在页面上显示帖子的方式

<?php
$posts = show_posts1($_SESSION['user_id']);
if (count($posts)){
?>
    <table>
<?php
        foreach ($posts as $key => $list){
            echo "<tr>";
            echo "<td>".$list['body']."
                 </td>";
            echo "</tr>";
        }
?>
    </table>
<?php
}else{
?>
    <p><b>You haven't posted anything yet!</b></p>
<?php
}
?>

您的查询应如下所示,以便对结果进行分页

$sql = "SELECT p.body, p.stamp, p.id,u.username, u.imagelocation 
    FROM posts p 
    INNER JOIN users u 
    ON p.user_id=u.id 
    WHERE p.user_id='$userid' 
    ORDER BY p.stamp DESC
    LIMIT <offset>,<max_rows>";

将当前偏移量存储在 Javascript 中,然后只需使用 AJAX 调用 show_posts1 ($userid[, $offset]) 再次运行查询并检索接下来的 20 个帖子。