【问题标题】:Showing more posts when a user clicks the show more button当用户点击显示更多按钮时显示更多帖子
【发布时间】:2012-04-10 08:13:03
【问题描述】:

当我网站上的用户访问他们的个人资料页面时,他们会看到他们发布的帖子列表。我目前默认显示 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
}
?>

【问题讨论】:

  • 如果您想运行更多查询,您将需要考虑使用 AJAX。
  • 我在这里没有看到你限制帖子的任何地方......

标签: php mysql function


【解决方案1】:

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

$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 个帖子。

【讨论】:

  • 我可以只使用 $("#feeds").load("feeds.html"); jquery 关键字。 feeds.html 包含带有适当偏移量和限制信息的查询?否则ajax代码是什么样的?每次用户单击显示更多时,我还需要动态更改 javascript 中的当前偏移量吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-20
  • 2021-09-18
  • 2018-04-28
  • 2021-10-17
  • 1970-01-01
相关资源
最近更新 更多