ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

使用glob的PHP分页来检索图像

2019-10-09 11:34:44  阅读:152  来源: 互联网

标签:php glob pagination


我正在尝试使用glob()从文件夹中检索图像,我希望将其分页,因此每页仅显示3张图像.

从在互联网上以及在S.O.我有下面的代码.问题在于它仅从目录中提取三个图像,当我单击下一页时,它会显示相同的3个图像.

使用glob为每个页面获取不同图像的最佳方法是什么?

        $imagesDir = 'uploadedImages/thumbs/*';
     $itemsPerPage = 3;
     $currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
     $totalItems = getTotalItems($imagesDir);
     $totalPages = ceil($totalItems / $itemsPerPage);

     getItemsFromPage($currentPage, $itemsPerPage, $imagesDir);
     getPager($totalPages, $currentPage);

     //counts the number of files in the dir
     function getTotalItems($imagesDir) {
      $numImages = count(glob($imagesDir));
      return $numImages;
     }

     //gets the files from the dir
     function getItemsFromPage($page, $numItems, $dir) {

      $x = 0;

      //get the first 3 items from the 
      foreach(glob($dir) as $image)
      {
       if($x < $numItems)
       {
        echo "<li><img src=\"$image\" alt=\"$image\" /></li>";
        $x++;
       }
       else
       {
        break;
       }
      }
     }

     //creates the pages and the links
     function getPager($totalPages, $currentPage) {

      // if we're not on the first page, show the previous page and first page links
      if($currentPage > 1)
      {
       $prevPage = $currentPage - 1;

       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";    
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevPage'><</a> ";
      }

      $range = 1;

      // show what page we're on and the page numbers surround this page
      for($x = ($currentPage - $range); $x < ($currentPage + $range) +1; $x++)
      {
       if(($x > 0) && ($x <= $totalPages)) 
       {
        if($x == $currentPage)
        {
         echo " [<b>$x</b>] ";
        }
        else
        {
         echo " <a href='{$_SERVER['PHP_SELF']}?currentPage=$x'>$x</a> ";
        }
       }
      }

      // if we're not on the last page show the next page and last page links
      if($currentPage != $totalPages)
      {
       $nextPage = $currentPage + 1;

       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextPage'>></a> ";
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalPages'>>></a> ";
      }
     }    

解决方法:

 function getItemsFromPage($page, $numItems, $dir) {

  $offset = ($page - 1) * $numItems;
  $x = 0;

  //get the first 3 items from the 
  foreach(glob($dir) as $image)
  {
   if($x < $offset)
   {
    $x++;
   }
   elseif($x < $numItems + $offset)
   {
    echo "<li><img src=\"$image\" alt=\"$image\" /></li>";
    $x++;
   }
   else
   {
    break;
   }
  }
 }

标签:php,glob,pagination
来源: https://codeday.me/bug/20191009/1878942.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有