【问题标题】:PHP pagination using glob to retrieve images使用 glob 检索图像的 PHP 分页
【发布时间】:2011-03-09 22:54:42
【问题描述】:

我正在尝试使用 glob() 从文件夹中检索图像,并且我希望它被分页,因此每页仅显示 3 个图像。

从互联网和 S.O.我有下面的代码。问题是它只从目录中提取三张图片,当我点击下一页时,它显示相同的三张图片。

使用 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> ";
      }
     }    

【问题讨论】:

    标签: php pagination glob


    【解决方案1】:
     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;
       }
      }
     }
    

    【讨论】:

    • 这个会在页面上添加新图片,越来越多:)
    • 应该最多添加 $numItems 张图片。
    • 天哪!它使用空循环
    【解决方案2】:

    问题出在 getItemsFromPage 函数中。 它总是从$x = 0; 开始,而 $x 应该是不同的,取决于页码

    【讨论】:

      【解决方案3】:

      如果有人想检索没有文件扩展名的图像文件名 使用pathinfo - 返回有关文件路径的信息。

       $dirname = "cards/";
       $images = glob($dirname . "*.png");
       foreach ($images as $image) {
           $title = pathinfo($image);
           echo $title['filename'];
      }
      

      【讨论】:

        猜你喜欢
        • 2013-01-22
        • 1970-01-01
        • 1970-01-01
        • 2011-09-06
        • 2011-08-25
        • 1970-01-01
        • 2015-09-17
        • 2015-11-20
        • 2013-09-26
        相关资源
        最近更新 更多