0

無限スクロールの場合にページを動的にロードするにはどうすればよいですか。

問題は、phpファイルを手動で作成する必要があることです。スクリプトは2.php、3.php、4.phpなどを検索します...これらのページを手動で作成したくないのですが、自動にしたいですか?:)

新しい画像を取得するには、ある種のループを作成する必要があると感じています。現在、私のコードは次のとおりです

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
  <title>Adi</title>

  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Adi</title>

<link rel="stylesheet" type="text/css" href="style.css" />


<script src="jquery-1.7.1.min.js"></script>
<script src="jquery.masonry.min.js"></script>
<script src="jquery.infinitescroll.min.js"></script>
<script src="modernizr-transitions.js"></script>
</head>

<body>
<div id="container" class="transitions-enabled infinite-scroll clearfix">
<?php
// Make a MySQL Connection
mysql_connect("localhost", "ID", "Pass") or die(mysql_error());
mysql_select_db("DB") or die(mysql_error());

// Retrieve all the data from the "test " table
$result = mysql_query("SELECT * FROM test limit 10")
or die(mysql_error());  

// store the records of the "TABLENAME" table into $row array and loop through
while ( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) ) {

// Print out the contents of the entry 

//echo "details: ".$row['id'];
echo '<div class="box">';
echo "<img src=\"images/".$row['image']."\" alt=\"name\" />";
echo '</div>';
}
?>
</div>
<p>Hi</p>
<p>&nbsp;</p>

<nav id="page-nav">
  <a href="2.php"></a>
</nav>

<script >
  $(function(){
    var $container = $('#container');

    $container.imagesLoaded(function(){
      $container.masonry({
        itemSelector: '.box',
        columnWidth: 60
      });
    });

    $container.infinitescroll({
      navSelector  : '#page-nav',    // selector for the paged navigation 
      nextSelector : '#page-nav a',  // selector for the NEXT link (to page 2)
      itemSelector : '.box',     // selector for all items you'll retrieve
      loading: {
          finishedMsg: 'No more pages to load.',
          img: 'http://i.imgur.com/6RMhx.gif'
        }
      },
      // trigger Masonry as a callback
      function( newElements ) {
        // hide new items while they are loading
        var $newElems = $( newElements ).css({ opacity: 0 });
        // ensure that images load before adding to masonry layout
        $newElems.imagesLoaded(function(){
          // show elems now they're ready
          $newElems.animate({ opacity: 1 });
          $container.masonry( 'appended', $newElems, true ); 
        });
      }
    );

  });
</script>
</body>
</html>

ページ2.php

<?php
    // Make a MySQL Connection
    mysql_connect("localhost", "ID", "Pass") or die(mysql_error());
    mysql_select_db("DB") or die(mysql_error());

    // Retrieve all the data from the "test " table
    $result = mysql_query("SELECT * FROM test limit 5")
    or die(mysql_error());  

    // store the records of the "TABLENAME" table into $row array and loop through
    while ( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) ) {

    // Print out the contents of the entry 

    //echo "details: ".$row['id'];
    echo '<div class="box">';
    echo "<img src=\"images/".$row['image']."\" alt=\"name\" />";
    echo '</div>';
    }
    ?>
4

1 に答える 1

1

次のような引数を取る単一のページを作成し、それview.php?start=100&end=150を反映するようにクエリを変更します。

SELECT * FROM test LIMIT <escaped and sanitized "start">, <difference between "start" and "end">

start と end の違いは、各ページに表示する要素の数を反映するために渡される値である場合もあります。

次に、JQuery を使用して、ページの下部がユーザーのビューポートに表示されるタイミングを感知し、AJAX を使用してサーバーに次の画像セットを提供するように依頼できます。

したがって、シーケンスは次のようになります。

  • ユーザーがページをロードします ( view.php)。スクリプトは、ページの完全な HTML、要素 0 ~ 10 などを返します。
  • ユーザーはページの一番下までスクロールします。あなたのJQueryはこれが起こるのを見て言います$.post('view.php?ajax=true', { start: end+1, end: end+11 }, someFunctionThatResetsEndToReflectTheNewEndAndUpdateThePage );
  • スクリプトは、それが AJAX 要求 (注: ajax=true) であったことを認識し、表示する必要があるデータだけを含む XML または JSON を返します。これを行うための別のスクリプトを作成することもできます。
  • あなたのJavaScript関数(上記の非常に長い名前の関数)はデータを処理し、ユーザーが理解できるものに処理します
于 2012-04-22T05:47:08.693 に答える