-3

我在 MYSQL DB 中有 1000 张图片我想以 pinterest.com 的方式排列它们

我遇到了砌体 jquery 脚本。虽然代码讲述了 HTML 文件的用法,但我找不到将它与 PHP 一起使用的方法。

http://masonry.desandro.com/demos/infinite-scroll.html

如果我回显所有图像,则使用 PHP,那么页面将非常沉重。当我到达底部滚动条时,如何集成 Masonry 以从 MYSQL DB 中获取图像,从而使页面变亮?

我对此有点困惑,我不是 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

2

如果你想展示它们真的很好看这个:http: //isotope.metafizzy.co/ 它很容易集成,我自己用过。

你要做的就是这个。在你设置好所有同位素之后。(它将向您展示如何)您像这样在您的页面上设置一个 PHP 函数。

<div id="container">
    <?php
        $QUERY = "SELECT * FROM `IMAGES` ORDER BY `ID` Limit 20;"; // 20 Image Limit
        $RESULTS = mysql_query($QUERY);
        while($ROW = mysql_fetch_assoc($RESULTS)) {
            echo '<div class=" Your Main Name For The Isotope Objects "><img src="' . $ROW['URL'] . '" /></div>';
        }
    ?>
</div>

while() 将继续遍历您的数据库,直到达到其限制,为您提供 20 个包含图片的 div。

于 2012-04-21T16:42:56.417 回答