2010-06-30 2 views
1

Я пытаюсь получить изображения из папки с помощью glob(), и я хочу, чтобы она была разбита на страницы, поэтому она отображает только 3 изображения на страницу.PHP разбиение на страницы с использованием glob для извлечения изображений

От копания в Интернете и здесь, на С.О. У меня есть код ниже. Проблема в том, что он только вытягивает три изображения из каталога, и когда я нажимаю на следующей странице, он показывает те же 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> "; 
     } 
    }  

ответ

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; 
    } 
    } 
} 
+0

это будет добавление новых изображений на страницу, больше и больше :) –

+0

Он должен добавить не более $ numItems изображений. – amccausl

+0

OMG получил это! он использует пустые циклы –

0

проблема в функции getItemsFromPage. Он всегда начинается с $x = 0; а $ х должны быть варьироваться, в зависимости от номера страницы

0

если кто-то хочет получить имя файла изображения без расширения файла использования pathinfo - Возвращает информацию о пути к файлу.

$dirname = "cards/"; 
$images = glob($dirname . "*.png"); 
foreach ($images as $image) { 
    $title = pathinfo($image); 
    echo $title['filename']; 
} 
Смежные вопросы