![](/img/trans.png)
[英]How to dynamically change images on a loaded page with a link attached to each images
[英]How to change images dynamically
在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>
现在,我需要动态更改图像。 我需要显示服务器文件夹中包含的图像。 在当前服务器上,我无法使用数据库,但可以访问另一台服务器,在该服务器上可以使用数据库存储图像标题和路径。 我该怎么做才能更改以前的代码,以允许我更改存储在文件夹中或数据库中的图像? 解决我的问题的最佳方法是什么?
为简便起见,我决定在服务器中创建一个文件夹以上传图像,然后使用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>";
}
?>
可能有更好的解决方案,但这对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.