2014-08-28 82 views
-1

我無法在網絡上的任何位置找到答案(適用)。燈箱顯示目錄中的多個圖像

我想讓Lightbox從一個目錄加載圖像,因爲它們會經常更新。

如果任何人都可以糾正我做錯了什麼,或者有一個解決方案使用PHP或由特定目錄自動填充的隱藏div,將不勝感激。

這是我想出來的,但似乎沒有工作;

<?php $dirname = "img/love/"; $images = glob($dirname."*.jpg"); foreach($images as $image) { echo '<img data-lightbox="love" src="'.$image.'" /><br />'; } ?> 

,這裏是我的測試頁:http://knowledgeoverfame.com/tslp/leet/alt/index.html

我沒有在這裏找到任何類似的問題有一個答案,但如果我可能已經錯過了它,請賜教:)

謝謝!

+0

請詳細說明更多...你想做什麼... – Dinesh 2014-08-28 04:09:58

+0

我有一個代碼。如果在我到達電腦的時候沒有回答,我會發布它。 – 2014-08-28 04:10:25

+0

嘗試使用雙引號echo – 2014-08-28 04:11:42

回答

0

嘗試使用scandir()來獲取圖像文件的數組。

例子:

<?php 
    $images = scandir($your_folder); 
    foreach ($images as $key => $filename) { 
    if ($key > 1) { //ignores the first two values which refer to the current and parent directory 
    echo '<img data-lightbox="love" src="'.$your_folder."/".$filename.'" /><br />'; 
    }  
    } 
?> 

供參考:PHP scandir()

+0

所以我會添加到我已有的,或者這將取代我現在的代碼? – tslpdsgn 2014-08-28 06:30:12

+0

用這個替換你的代碼。 – qtgye 2014-08-28 07:16:36

+0

我得到它的工作,不得不編輯一點,但感謝一百萬! – tslpdsgn 2014-08-28 23:34:11

0

你想要做什麼 - 使用燈箱顯示從文件夾的圖像 - 是已出現不同程度的解決了很多次的問題複雜。如果你做一個像「php圖片庫」的谷歌搜索,你會發現示例腳本。一年前我做了這件事,經過一些實驗後,我選擇了Minigal Nano,因爲它是一個強大的腳本,但很簡單,它很容易看到它是如何工作的,然後返工以供我自己使用。我也發現這是一種學習php的有效方法。

0

該腳本將從目錄中提取圖像。我用fancybox,但你可以修改,以滿足您的需求。

<?php 
$directory = 'yourDirectory/gallery/thumbs'; //where the gallery thumbnail images are located  
$files = glob($directory."/*.{jpg,jpeg,gif,png}", GLOB_BRACE); 
natsort($files); //sort by filename 
?> 

<?php 
for($x = 0; $x < count($files); $x++){ 
$thumb = $files[$x]; 
$file = basename($thumb); 
$nomargin = $x%4 == 0?" nomargin":""; 
$title = htmlspecialchars($file); 
?> 
    <div class="thumbs fancybox<?= $nomargin ?>" style="background:url('<?= $thumb ?>') no-repeat 50% 50%;"> 
     <a rel="group" href="yourDirectory/gallery/<?= $file ?>"></a> 
    </div> 
<?php 
}//end for loop 
?> 
+0

這是我想出的,它似乎在做伎倆... ... <?php $ dirname =「img/love」; $ images = scandir(「./ img/love /」); foreach($ images = $ key => $ filename){ if($ key> 1){//忽略引用當前和父目錄的前兩個值 echo'

'; } } ?> – tslpdsgn 2014-08-28 23:11:05

+0

這是感謝@Jacelysh稍微調整了一下......你預見到了這個問題嗎?我很好奇,我不太流利的PHP,所以我有點不知所措,直到它工作哈哈 你可以檢查http://knowledgeoverfame.com/tslp/leet/alt並點擊愛鏈接來查看它在行動... – tslpdsgn 2014-08-28 23:15:30

+0

它適合我。 – 2014-08-28 23:37:31