2015-02-07 78 views
0

在一個HTML文件,我使用下面的代碼的彈出窗口上顯示的圖像:如何改變圖像的動態

<div id="divCheckbox" style="display: none;"> 
       <a class="fancybox" rel="gallery01" title= "BONOS ALQUILER 2/6" href="eventos/Bonos_alquiler.jpg">one</a> 
       <a class="fancybox" rel="gallery01" title= "CICLO 3/6" href="eventos/Ciclo.jpg">one</a> 
       <a class="fancybox" rel="gallery01" title= "PEQUE TENIS/PADEL 4/6" href="eventos/PEQUES-padel_tenis.jpg">one</a> 
       <a class="fancybox" rel="gallery01" title= "PILATES 5/6" href="eventos/Pilates.jpg">one</a> 
       <a class="fancybox" rel="gallery01" title= "ZUMBA 6/6" href="eventos/Zumba.jpg">one</a> 
</div> 

現在,我需要的是動態改變的圖像。 我需要顯示服務器文件夾中包含的圖像。 在當前服務器上,我無法使用數據庫,但是我可以訪問另一臺服務器,我可以使用數據庫來存儲圖像標題和路徑。 我可以做些什麼來改變以前的代碼,以允許我更改存儲在文件夾中或存儲在數據庫中的圖像? 什麼是我的問題的最佳解決方案?

+2

您需要使用ajax。寫一個PHP文件,從數據庫中獲取圖像url並輸出帶有url的json響應。發送一個Ajax請求到php文件。取出json解碼它並更改圖像文件的URL。 – 2015-02-07 20:25:39

+0

@codename_subho,謝謝你的評論。 – mvasco 2015-02-07 20:28:08

回答

0

爲了方便起見,我決定在服務器上創建一個文件夾來上傳圖像,然後使用PHP讀取該文件夾中的文件。這是我的解決方案,取自Github:

<?php 
    # To prevent browser error output 
    header('Content-Type: text/javascript; charset=UTF-8'); 
    # Path to image folder 
    $imagefolder = 'eventos/'; 
    # Show only these file types in the image folder 
    $imagetypes = '{*.jpg,*.JPG,*.JPEG,*.png,*.PNG,*.gif,*.GIF}'; 
    # Add images to array 
    $images = glob($imagefolder.$imagetypes, GLOB_BRACE); 
    # Sort the images based on its 'last modified' time stamp 
    $sortedImages = array(); 
    $count = count($images); 
    for ($i = 0; $i < $count; $i++) { 
     $sortedImages[date ('YmdHis', filemtime($images[$i])).$i] = $images[$i]; 
    } 
    # Set to 'false' if you want the oldest images to appear first 
    $newest_images_first = true; 
    # Sort images in array 
    if($newest_images_first) { 
     krsort($sortedImages); 
    } else { 
     ksort($sortedImages); 
    } 

    foreach ($sortedImages as $image) { 
     # Get the name of the image, stripped from image folder path and file type extension 
     $name = 'Polideportivo Los Cedros: '.substr($image,strlen($imagefolder),strpos($image, '.')-strlen($imagefolder)); 

    echo " <a class='fancybox' rel='gallery01' title= '".$name."' href='".$image."'></a>"; 

    } 

?> 

可能有更好的解決方案,但這對我很有用。