2012-04-12 19 views
0

I持つフォルダの内容を読み取り、次のコードエコーPHPの配列値

PHPコード:

この次のコードを吐く
<? 
//PHP SCRIPT: getimages.php 
header('content-type: application/x-javascript'); 

//This function gets the file names of all images in the current directory 
//and ouputs them as a JavaScript array 
function returnimages($dirname="./images") { 
    $pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions 
    $files = array(); 
    $curimage=0; 
      if($handle = opendir($dirname)) { 
       while(false !== ($file = readdir($handle))){ 
        if(eregi($pattern, $file)){ //if this file is a valid image 
//Output it as a JavaScript array element 
        echo 'galleryarray['.$curimage.']="'.$file .'";' . "\n"; 
      $curimage++; 
        } 
      } 

      closedir($handle); 
} 
     sort($files); 
     return($files); 
} 

echo 'var galleryarray=new Array();' . "\n"; //Define array in JavaScript 
returnimages() //Output the array elements containing the image file names 

?> 

:今すぐ

var galleryarray = new Array(); 
galleryarray[0] = "image1.jpg"; 
galleryarray[1] = "image5.jpg"; 
galleryarray[2] = "image2.jpg"; 
galleryarray[3] = "image4.jpg"; 
galleryarray[4] = "image8.jpg"; 
galleryarray[5] = "image7.jpg"; 
galleryarray[6] = "image0.jpg"; 
galleryarray[7] = "image3.jpg"; 
galleryarray[8] = "image6.jpg";​ 

を私のhtmlファイルは、私が

<img src="images/image0.jpg" /> 
<img src="images/image1.jpg"/> 
... 
... 
のようなものをエコーし​​たい

どうすればいいですか?

答えて

1
foreach($galleryarray as $img) { 
    echo "<img src='images/$img' />"; 
} 
0
foreach($galleryarray as $gallery) echo '<img src="images/' . $gallery . '" />'; 
0

これに機能してエコーラインを変更してみてください:これは:)

OR関数の外

、使用するのに役立ちます

echo '<img src="images/' . $file . '" />' ; 

・ホープそれは次のようなものです:

$images = returnimages(); //will get the array containing the images 
foreach($images as $img) 
{ 
    echo '<img src="images/' . $img . '" />'; 
} 

また、あなたは、whileループであれば条件内でこの行を含める必要があります:

$files[] = $file; //insert the file into the array. This array is what we are going to return from the function 

更新

私はこのコードをテストしているし、それが働いている:

//PHP SCRIPT: getimages.php 
    header('content-type: application/x-javascript'); 

    function returnimages($dirname="./images") { 
     $pattern="([^\s]+(\.(?i)(jpg|png|gif|bmp))$)";  // http://www.mkyong.com/regular-expressions/how-to-validate-image-file-extension-with-regular-expression/ 
     $files = array(); 
     if($handle = opendir($dirname)) { 
      while(false !== ($file = readdir($handle))){ 
       if(preg_match($pattern, $file)){ //if this file is a valid image 
        $files[] = $file; 
       } 
      } 

      closedir($handle); 
     } 
     //sort($files);   // http://php.net/manual/en/function.sort.php 
     natcasesort($files); // case insensitive "natural order" algorithm :: http://php.net/manual/en/function.natcasesort.php 

     return($files); 
    } 

    $images = returnimages(); //will get the array containing the images 
    foreach($images as $img) 
    { 
     echo '<img src="images/' . $img . '" />'; 
    } 

私のimagesフォルダには、テスト用に5つのファイルがあります。そして、それを実行すると、それは次のように与える:

<img src="images/a.jpg" /> 
<img src="images/ba.jpg" /> 
<img src="images/ca.jpg" /> 
<img src="images/Copy (3) of a.jpg" /> 
<img src="images/Copy (4) of a.jpg" /> 
+0

とどのように私はそれをソートしないでforeachの詳細は? – user1325414

+0

私は答えを更新しました。:) –

+0

アルファベット順にファイル名を並べ替える方法はありますか? – user1325414

0

多分このヘルプU

$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions 
      if($handle = opendir("./images")) { 
       while(false !== ($file = readdir($handle))){ 
        if(eregi($pattern, $file)){ ?> 
        <img src="/images/<?php echo $file; ?>"> 
      <?php 
        } 
      } 
      } 
0

あなたは値によってイメージ名をソートした後、画像を表示することを想定します。最初

asort($galleryarray); 
    foreach ($galleryarray as $key => $val) { 
     echo "<img src='images/{$val}'/>" 
    } 

印刷したい場合は、配列

1)しますprint_r($配列) "ASORT" 機能を使用して値をソートする必要があります。あなたはきれいにフォーマットされた配列をしたい場合や、その後

echo '<pre>'; print_r($array); echo '<pre/>'; 

2)は、データ型と長さのような、アレイ内のコンテンツのより多くの情報を取得するためにvar_dump($array)を使用しています。

3)あなたは(ループ配列使用して、PHPのforeachの缶)。所望の出力を得る。 PHPのマニュアルのウェブサイトhttp://in3.php.net/manual/en/control-structures.foreach.php

関連する問題