dongmoyu0336 2016-06-04 08:03
浏览 31
已采纳

如何限制我的图库一次显示的行数

I have a gallery that is generated from a directory with php.

I'd like to show only 2 rows of 4 images each, so a total of 8 images. There would be a "show more" button at the bottom.

When the user would click the button then 2 more rows would load.

How can I limit the number of rows?

Would jQuery be the way to go?

<div class="w3-content w3-container w3-padding-64" style="margin-top:50px;">
  <h3 class="w3-center w3-text-white">Never Ending Light of Day</h3>
  <br>

  <!-- Responsive Grid. Four columns on tablets, laptops and desktops. Will stack on mobile devices/small screens (100% width) -->
  <div class="w3-row-padding w3-center">
<?php
// Find all files in that folder
$files = glob('uploads/*');

// Do a natural case insensitive sort, usually 1.jpg and 10.jpg would come next to each other with a regular sort
natcasesort($files);


// Display images
foreach($files as $file) {
   echo '<div class="w3-col m3"><img src="' . $file . '" style="width:100%" onclick="onClick(this)" class="w3-hover-opacity"/></div>';
}

?>
    
  </div>
</div>

</div>
  • 写回答

2条回答 默认 最新

  • dongling4383 2016-06-04 08:15
    关注

    There are two questions here :

    How to limit the number of rows created by the php script

    You'd need to splice you $files array with array_splice($files, 0, 8)

    <?php
    // Find all files in that folder
    $files = glob('uploads/*');
    
    // Do a natural case insensitive sort, usually 1.jpg and 10.jpg would come next to each other with a regular sort
    natcasesort($files);
    
    // get only 8 files
    $files = array_splice($files, 0, 8);
    
    
    // Display images
    foreach($files as $file) {
       echo '<div class="w3-col m3"><img src="' . $file . '" style="width:100%" onclick="onClick(this)" class="w3-hover-opacity"/></div>';
    }
    
    ?>
    

    Then you can "paginate" the images list with an $offset variable that can be generated from request params :

    $offset = $_GET["images_offset"] || 0; // should be a multiple of 8
    $files = array_splice($files, $offset, 8);
    

    How to load new images when the user clicks the button

    jQuery could be the way to go to load asynchronously.

    var offset = 0;
    var url = "yourpageurl";
    $('#yourbuttonidoranyselector').bind('click', function(){
      offset += 8;
      $.ajax(url + "?images_offset=" + offset, function(data){
        // append the new lines to the chosen container
      });
    });
    

    This is just a bit of the logic you should explore. Don't just copy and paste the code please.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看