2012-01-07 64 views
0

也許這是一個愚蠢的問題,但我無法自己解決它。重複PHP讀取代碼

我有以下代碼:

<?php 
    $path = "galeria01"; 
    $dir_handle = @opendir($path) or die("Not found: $path"); 
    list_dir($dir_handle,$path); 

    function list_dir($dir_handle,$path) 
    { 
     global $div; 
     $div = 001; 
     global $zindex; 
     $zindex = 200; 
     global $margem; 
     $margem = 114; 
     while ((($file = readdir($dir_handle)) !== false)) { 
      if ($file != "." && $file != "..") { 
       echo PHP_EOL . '<div id="'; 
       echo str_pad($div, 3, 0, STR_PAD_LEFT); 
       echo '" style="position:absolute;left:'; 
       echo $margem; 
       echo 'px;z-index:'; 
       echo $zindex; 
       echo '"><img src="galeria01/'; 
       echo $file; 
       echo '" width="675" height="450" /></div>'; 
       echo'<span class="clear"></span>'; 
       $div++; 
       $zindex--; 
       $margem = $margem - 675; 
      } 
     } 
    } 
    closedir($dir_handle); 
?> 

正如你可以看到,它讀取一個文件夾中的所有文件,並生成以下代碼:

<div id="001" style="position:absolute;left:114px;z-index:200"><img src="001.jpg" width="675" height="450" /></div><span class="clear"></span> 
<div id="002" style="position:absolute;left:-561px;z-index:199"><img src="002.jpg" width="675" height="450" /></div><span class="clear"></span> 
<div id="003" style="position:absolute;left:-1236px;z-index:198"><img src="003.jpg" width="675" height="450" /></div><span class="clear"></span> 
<div id="004" style="position:absolute;left:-1911px;z-index:197"><img src="004.jpg" width="675" height="450" /></div><span class="clear"></span></div> 

我只需要重新運行該代碼幾次並生成所有這些dinamically生成的div,以相同的順序再次,但總是減少左邊距和z-索引值,如下所示:

<div id="001" style="position:absolute;left:114px;z-index:200"><img src="001.jpg" width="675" height="450" /></div><span class="clear"></span> 
        (...) 
    <div id="004" style="position:absolute;left:-1911px;z-index:197"><img src="004.jpg" width="675" height="450" /></div><span class="clear"></span></div> 
    <div id="001" style="position:absolute;left:-2586px;z-index:196"><img src="001.jpg" width="675" height="450" /></div><span class="clear"></span> 
        (...) 

我該怎麼做?

我希望它很簡單,你可以幫助我。

謝謝。

+1

不,這是沒有任何意義。 「從1到4比從1到4」?咦? – 2012-01-07 22:10:08

+2

(HTML id值不能以數字開頭)。對於這種我通常稱爲朋友[mo-du-lus](http://php.net/manual/en/language.operators.arithmetic.php) 。 – hakre 2012-01-07 22:11:32

+1

同意,你的問題沒有道理......也許你應該隔離問題並簡化問題 – Prof83 2012-01-07 22:17:57

回答

1

要重新運行代碼,您可以再次執行代碼。

除此之外,您需要將當前全局變量初始化從函數內部移動到全局範圍,以允許在代碼的額外重新運行時修改它們。

下面是修改後的代碼有一些意見:

<?php 
$path = "."; 
$dir_handle = @opendir($path) or die("Not found: $path"); 

// do the init of these variables outside of the function and before the first call 
global $zindex; 
$zindex = 200; 
global $margem; 
$margem = 114; 

// now call your function, it will behave like your original code 
list_dir($dir_handle,$path); 

// now call the your function again, it will pick up with the zindex/margem values of the last div you printed 
list_dir($dir_handle,$path); 

function list_dir($dir_handle,$path) 
{ 
    global $div; 
    $div = 001; 
    global $zindex; 
    // no longer set a new value for the zindex with each function call 
    //$zindex = 200; 
    global $margem; 
    // no longer set a new value for the margem with each function call 
    //$margem = 114; 
    //reset the directory handle to the first position 
    rewinddir($dir_handle); 
    while ((($file = readdir($dir_handle)) !== false)) { 
     if ($file != "." && $file != "..") { 
      echo PHP_EOL . '<div id="'; 
      echo str_pad($div, 3, 0, STR_PAD_LEFT); 
      echo '" style="position:absolute;left:'; 
      echo $margem; 
      echo 'px;z-index:'; 
      echo $zindex; 
      echo '"><img src="galeria01/'; 
      echo $file; 
      echo '" width="675" height="450" /></div>'; 
      echo'<span class="clear"></span>'; 
      $div++; 
      $zindex--; 
      $margem = $margem - 675; 
     } 
    } 
} 
closedir($dir_handle); 
?> 
+0

我能夠理解你的想法,完全按照你所說的去做,但可惜的是,輸出結果只會一次列出文件,就像以前一樣。 我調用函數的次數並不重要,它只在HTML上列出一次。 – OleSchmitt 2012-01-08 11:49:02

+0

這是因爲dir_handle指向第一個函數調用結束後的末尾。我修改了我在使用rewinddir()之前讀取目錄內容的答案...因此總是從頭開始讀取完整的目錄樹 – favo 2012-01-08 11:59:03

+0

是的!!!!!你搖滾,@favo!非常感謝。 – OleSchmitt 2012-01-08 12:40:44