2012-08-13 31 views
0

我在网页编码新的,我有问题分页列出的文件...从目录

现在我可以addapt列出,并从目录相呼应的图像,以显示他们的代码灯箱。

眼下代码显示所有图像从目录中,在5个图像的行,但无极限......

我怎么能我的代码更改显示的结果,可以说,例如:只有一行,每页有5个图像。然后显示通常的“页面:1,2,3 ...下一个...”我真的不知道该怎么做...我一直在尝试很多事情,但没有成功......

代码如下:

<?php 

$page = $_SERVER['PHP_SELF']; 

// How many images per row 
$maxCols = 5; 

// Directory where the albums are stored 
$base = "albums"; 

// Get album title 
$get_album = $_GET['album']; 


if (!$get_album) 
{ 
    $handle = opendir($base); 

    echo "<div id='albums' class='imageRow'>"; 

    while (($file = readdir($handle))!==FALSE) 
    {  
     if (is_dir($base."/".$file) && $file != "." && $file != "..") 
     { 
      //$list []= $file; 
      $img = $file.".jpg"; //Main image from each album. 

      echo "<div class='image'><a href='$page?album=$file'><img src='thumbnails.php?img=$base/$img' alt='$file' /></a><div class='album'><i>$file</i></div></div>"; 
     }  
    } 

    echo "</div>"; 

    closedir($handle); 

    //$total = count($list); 

} 
else 
{ 
    if (!is_dir($base."/".$get_album) || strstr($get_album,".")!=NULL || strstr($get_album,"/")!=NULL || strstr($get_album,"\\")!=NULL) 
    { 
     echo "Album doesn't exist."; 
     echo "<p /><a href='$page'>Back to albums</a>"; 
    } 
    else 
    { 
     $count = 0; 
     $handle = opendir($base."/".$get_album); 

     echo "<div id='images' class='imageRow'>"; 

     while (($file = readdir($handle)) !== FALSE) 
     { 
      if ($file != "." && $file !== "..") 
      { 
       echo "<div class='image'><a href='$base/$get_album/$file' rel='lightbox[1]' title='$file'><img src='thumbnails.php?img=$base/$get_album/$file' alt='$file' /></a></div>"; 

       $count++; 

       if($count == $maxCols) 
       { 
        echo "</div>"; 
        echo "<div id='images' class='imageRow'>"; 
        $count = 0; 
       } 

       $list[] = $file; //Assign the images to a list to allow count. 
      } 
     } 

     echo "</div>"; 

     closedir($handle); 

     $total = count($list); //Total elements at the album. 

     //echo "<a href='$page'>Back to albums</a>"; 
    } 
} 

?> 

任何帮助将不胜感激!

非常感谢提前!

+0

试试这个http://www.codehive.net/PHP-Array-Pagination-10.html – Cups 2012-08-13 17:17:45

+0

感谢您的回答,我似乎无法使用该代码来达到我的目的,我可以对此进行分页,但仍然显示我的目录中的每个文件。 – qalbiol 2012-08-13 19:34:41

回答

0

一般的方法是计算可用文件的总数(您当前正在执行的操作)。然后决定你要显示的每页文件的大小(在你的案例5中)。将计数除以该值以显示每页,以确定需要多少页(即,“1,2,3,下一个”输出应该是什么)。然后让每个链接为1,2,3,然后使用传递的参数链接回同一页面。因此,例如,页面2的链接可能是/yourscript.php?page=2

然后,您将使用页面偏移量来了解要输出的文件阵列中的哪个位置。因此,从页面= 2的示例中,您将开始考虑数组偏移5(因为文件0-4位于第一页)。

+0

嗨,谢谢你的回答,我知道分页的想法,实际上我编写了一个可以获得MySQL注册的分页系统,并且完美地工作。现在的问题是,我正在通过扫描目录来获取结果,并输出该目录中的每个文件。我试图找出如何添加适应我的分页系统,但我似乎无法实现它 – qalbiol 2012-08-13 17:47:27

0

试试这个: -

输出

enter image description here

<?php 

$maindir = "img" ; 
$mydir = opendir($maindir) ; 
$limit = 5; 
$offset = ((int)$_GET['offset']) ? $_GET['offset'] : 0; 
$files = array(); 
$page=''; 
$exclude = array(".", "..", "index.php",".htaccess","guarantee.gif") ; 
while($fn = readdir($mydir)) 
{ 
    if (!in_array($fn, $exclude)) 
    { 
     $files[] = $fn;; 
    } 
} 
closedir($mydir); 
sort($files); 
$newICounter = (($offset + $limit) <= sizeof($files)) ? ($offset + $limit) : sizeof($files); 

for($i=$offset;$i<$newICounter;$i++) { 
?> 
    <a href="<?php print $files[$i]; ?>"><?php print $files[$i]; ?></a><br> 
<?php 
} 
freddyShowNav($offset,$limit,sizeof($files),""); 

function freddyShowNav($offset, $limit, $totalnum, $query) { 
    global $PHP_SELF; 
    if ($totalnum > $limit) { 
      // calculate number of pages needing links 
      $pages = intval($totalnum/$limit); 

      // $pages now contains int of pages needed unless there is a remainder from division 
      if ($totalnum%$limit) $pages++; 

      if (($offset + $limit) > $totalnum) { 
       $lastnum = $totalnum; 
       } 
      else { 
       $lastnum = ($offset + $limit); 
       } 
      ?> 
       <table cellpadding="4"><tr><td>Page </td> 
      <?php 
      for ($i=1; $i <= $pages; $i++) { // loop thru 
       $newoffset=$limit*($i-1); 
       if ($newoffset != $offset) { 
      ?> 
        <td> 
         <a href="<?php print $PHP_SELF; ?>?offset=<?php print $newoffset; ?><?php print $query; ?>"><?php print $i; ?> 
         </a> 
        </td> 
      <?php 
        }  
       else { 
      ?> 
        <td><?php print $i; ?></td> 
      <?php 
        } 
       } 
      ?> 
        </tr></table> 
      <?php 
     } 
    return; 
    } 



echo $page; 
?> 
+0

谢谢,但太长和复杂,以适应我的代码加上它做了什么我需要的不同。 ;) – qalbiol 2012-08-13 18:02:28

0

DirectoryIteratorLimitIterator是我最好的朋友,虽然glob似乎更容易预过滤。您也可以编写自定义FilterIterator。需要PHP> 5.1,我想。

无前置:

$dir_iterator = new DirectoryIterator($dir); 
$paginated = new LimitIterator($dir_iterator, $page * $perpage, $perpage); 

水珠前置:

$dir_glob = $dir . '/*.{jpg,gif,png}'; 

$dir_iterator = new ArrayObject(glob($dir_glob, GLOB_BRACE)); 
$dir_iterator = $dir_iterator->getIterator(); 
$paginated = new LimitIterator($dir_iterator, $page * $perpage, $perpage); 

然后,做你的事:

foreach ($paginated as $file) { ... } 

注意,在DirectoryIterator示例的情况下,$file将是SplFileInfo的实例,而glob示例只是磁盘路径。