簡體   English   中英

使用glob的PHP分頁來檢索圖像

[英]PHP pagination using glob to retrieve images

我正在嘗試使用glob()從文件夾中檢索圖像,我希望將其分頁,因此每頁僅顯示3張圖像。

通過在Internet上進行挖掘以及在此處進行搜索,我得到了下面的代碼。 問題在於它僅從目錄中提取三個圖像,當我單擊下一頁時,它會顯示相同的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;
   }
  }
 }

問題出在getItemsFromPage函數中。 它總是從$x = 0; $ x應該有所不同,取決於頁碼

如果有人要檢索不帶文件擴展名的圖像文件名,請使用pathinfo返回有關文件路徑的信息。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM