【问题标题】:PHP MySQL pagination/infinite scroll issuePHP MySQL分页/无限滚动问题
【发布时间】:2015-07-04 19:51:34
【问题描述】:

我为我的网站创建了一个画廊,我决定在我的画廊上使用jQuery infinite scroll 插件(例如:facebook 墙)。

所有数据均来自 PHP MyAdmin 数据库。我的问题是,如何让数据从数据库中无限滚动?我的所有代码都可以正常工作。

  • 数据库:图库
  • 表的表结构:posts

index.php:

<?php
include ("connect_database.php");

$select_post = "SELECT * FROM posts ORDER BY rand() LIMIT 5";

$run_posts = mysql_query($select_post);

while ($row=mysql_fetch_array($run_posts)) {

  $post_id = $row['post_id'];
  $post_date = $row['post_date']; //.etc here
?>


<div id="container">

  <!-- gallery codes here and five thumbnails are randomly loading on homepage -->

</div>


<!--Next page for INFINITE SCROLL-->

<nav id="page-nav">
  <a href="next-page.php"></a> <!--please check script.js-->
</nav>

当我在&lt;nav&gt; 标签中添加 html 或 php 文件时,下一页正在加载,没有任何问题。但是我需要为每个加载创建一个新的 php/html 文件。我有一个想法来加载接下来的 5 个缩略图而不创建一个新文件。我需要从数据库中获取下五个缩略图以无限滚动。

script.js:

var $container = $('#container');
$container.infinitescroll({
    // infinite scroll options...
    navSelector  : "#page-nav",            
                   // selector for the paged navigation (it will be hidden)
    nextSelector : "#page-nav a:first",    
                   // selector for the NEXT link (to page 2)
    itemSelector : "#container .box",
                   // selector for all items you'll retrieve
    extraScrollPx: 10,
    }
);

【问题讨论】:

    标签: php mysql pagination infinite-scroll


    【解决方案1】:

    如果您希望每次滚动加载 5 个随机帖子,则无需区分一个请求与另一个请求(例如页码)。

    虽然我不认为这是一个可行的解决方案,因为您最终会得到重复的条目(您决定这是否是一个问题),您可以使用“路径”选项来调用下一页,如示例下面。

    $container.infinitescroll({
        // infinite scroll options...
        navSelector  : "#page-nav",            
                       // selector for the paged navigation (it will be hidden)
        nextSelector : "#page-nav a:first",    
                       // selector for the NEXT link (to page 2)
        itemSelector : "#container .box",
                       // selector for all items you'll retrieve
        extraScrollPx: 10,
        path: function(index){
           return "index.php?page=" + index;
         }
        }
    );
    

    当然,您将不得不调整您的 index.php 文件以正确处理页面参数。然而,再一次,有了随机结果,您将不需要“页面”参数,而是始终在路径函数中返回“index.php”,一切都应该正常。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-05
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多