2013-09-25 47 views
0

我有問題嘗試顯示使用php的數據庫的兩張照片的相冊。目前下面的代碼只適用於一張專輯。基本上我需要它來顯示'mount-everest-part-2'的照片。打開兩個文件夾和顯示圖像php

<?php 

$path = "images/galleries/"; 
$album = 'mount-everest-part-1'; 

if ($handle = opendir($path.$album.'/thumbs/')) { 
    while (false !== ($file = readdir($handle))) { 
     if ($file != "." && $file != ".." && substr($file, 0, 2) != '._') { 
      $files[] = $file; 
     } 
    } 
    closedir($handle); 
} 
asort($files); 

foreach($files as $file) { 
     echo '<li><a href="../' . $path . $album . '/images/' . $file . '" rel="shadowbox['.$album.']"><img src="../' . $path . $album . '/thumbs/' . $file . '" /></a></li>'; 
} 

?> 

如何使用此代碼打開兩個文件並使用相同的foreach循環吐出文件?

回答

1

這聽起來像是那OOP將適合的事情之一很好。這裏有一個例子:

<?php 
    class Album_Picture_File { 
     private $fileName; 
     private $path; 
     private $album; 

     public function __construct($fileName, $path, $album) { 
      $this->fileName = $fileName; 
      $this->path = $path; 
      $this->album = $album; 
     } 

     private function getAlbumPath() { 
      return '../' . $this->path . $this->album; 
     } 

     public function getPicturePath() { 
      return $this->getAlbumPath() . '/images/' . $this->fileName; 
     } 

     public function getThumbnailPath() { 
      return $this->getAlbumPath() . '/thumbs/' . $this->fileName; 
     } 
    } 

    function fetchFiles($path, $album) { 
     $files = array(); 
     if ($handle = opendir($path.$album.'/thumbs/')) { 
      while (false !== ($file = readdir($handle))) { 
       if ($file != "." && $file != ".." && substr($file, 0, 2) != '._') { 
        $fullPath = $path . $album . '/thumbs/' . $file; 
        $files[$fullPath] = new Album_Picture_File($file, $path, $album); 
       } 
      } 
      closedir($handle); 
     } 
     ksort($files); //sort after key (out file path) 
     return $files; 
    } 

    $files = array_merge(
     fetchFiles('images/galleries/', 'mount-everest-part-1'), 
     fetchFiles('images/galleries/', 'mount-everest-part-2') 
    ); 

    foreach($files as $file) { 
     echo '<li><a href="' . $file->getPicturePath() . '" rel="shadowbox['.$album.']"><img src="' . $file->getThumbnailPath() . '" /></a></li>'; 
    } 
?> 

注意,而不是推$files用繩子,我們與Album_Picture_File的對象推它。

+0

這完美的作品!謝謝。我的PHP技能是非常基本的,所以這是一個超過我的頭的方法:( –

+0

@RobMorris它使用類。類是可以單獨添加數據/函數的對象(以及其他東西,例如traits/static等等) 。,但現在不用擔心),並且它們可以包含它們自己的私有變量(這就是爲什麼'$ this-> fileName'每個文件都不相同的原因)我建議你閱讀[PHP OOP]( http://php.net/manual/en/language.oop5.php),因爲它一旦你瞭解它就很棒了 – h2ooooooo

+0

謝謝你的提示,我是前端開發人員,所以這對我來說有點嚇人!我想知道PHP和PHP的oop,但也許我需要一個像你這樣的老師;) –

0

創建一個全局數組:

$all = array(); 

然後,進行2個循環,推動全球陣列(創建一個函數讀取例如目錄)

$files_dir_one = getFiles("your_dir"); 
$files_dir_two = getFiles("your_dir2"); 

$all = array_merge($files_dir_one, $files_dir_two); 

function getFiles($directory) { 
$files = array(); 
if ($handle = opendir($directory)) { 
    while (false !== ($file = readdir($handle))) { 
     if ($file != "." && $file != ".." && substr($file, 0, 2) != '._') { 
      $files[] = $file; 
     } 
    } 
    closedir($handle); 

} 
//asort($files); 
return $files; 

} 

然後,最後一步:填充視圖

+0

對此進行了審查之後,還有點不同。這個問題最可能廣泛。但是,如果您想回答該問題:文件所在的相冊信息位於文件名旁邊,這樣可以填充視圖。我沒有時間提交正確的代碼,因此刪除了我的答案,因爲它不完整。 – hakre

0

如何使用此代碼打開兩個文件並使用相同的foreach循環吐出文件?

從字面上看,它的工作原理是這樣的。首先,提取與兩個輸入變量的函數:

/** 
* @param string $path 
* @param string $album 
*/ 
function list_images($path, $album) { 
    $files = []; 

    if ($handle = opendir($path . $album . '/thumbs/')) 
    { 
     while (false !== ($file = readdir($handle))) 
     { 
      if ($file != "." && $file != ".." && substr($file, 0, 2) != '._') 
      { 
       $files[] = $file; 
      } 
     } 
     closedir($handle); 
    } 
    asort($files); 

    foreach ($files as $file) 
    { 
     echo '<li><a href="../' . $path . $album . '/images/' . $file . '" rel="shadowbox[' . $album . ']"><img src="../' . $path . $album . '/thumbs/' . $file . '" /></a></li>'; 
    } 
} 

然後,你可以遍歷您的兩張專輯,並同時輸出:

$path = "images/galleries/"; 
$albums = ['mount-everest-part-1', 'mount-everest-part-2']; 

foreach ($albums as $album) 
{ 
    list_images($path, $album); 
} 
+0

可能在實例中開始使用'$ files = []'有點太早了,因爲它是一個5.4的特性,[*相應*只有〜8%使用5.4+](http://w3techs.com/technologies/details/ PL-PHP/5 /全部)? – h2ooooooo

相關問題