2013-09-25 2 views
0

PHP로 두 장의 사진 앨범을 표시하려고하는 문제가 있습니다. 현재 다음 코드는 하나의 앨범에서만 작동합니다. 기본적으로 '마운트 - 에베레스트 - 파트 -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가 맞는 것 그런 것들 중 하나 같은 소리 훌륭하게. 대신 문자열 $files을 누르면, 우리는 Album_Picture_File 객체로 밀어

<?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>'; 
    } 
?> 

주 : 다음은 예입니다.

+0

완벽하게 작동합니다! 고맙습니다. 내 PHP 기술은 매우 기본이므로 내 머리 위로하는 방법이다. ( –

+0

@RobMorris 이것은 클래스를 사용한다. 클래스는 데이터/함수를 개별적으로 추가 할 수있는 객체이다 (traits/static/etc 등과 같은 다른 것들도 포함한다). .,하지만 지금은 걱정하지 마라.) 그리고 그들 자신의 private 변수를 포함 할 수있다. (그래서'$ this-> fileName'은 파일마다 다르다.) 나는 [PHP OOP] http://php.net/manual/en/language.oop5.php) 일단 당신이 그것을 알게되면 놀랍습니다. – h2ooooooo

+0

팁 주셔서 감사합니다. 저는 프론트 엔드 데브입니다. 그래서 이것은 저에게 조금 무섭습니다! 나는 PHP와 php에 대해 알고 싶다. 어쩌면 너와 같은 선생님이 필요해.) –

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

5.4 기능과 [* 그에 따라 * 8 % 만 5.4+ 사용] (http://w3techs.com/technologies/details/) 예제에서'$ files = []'를 사용하기에는 너무 이르다. pl-php/5/all)? – h2ooooooo

관련 문제