如何在每个不同的图像上使用多个不同的href链接?

时间:2013-10-07 12:29:19

标签: php arrays image indexing href

如何在每个不同的图片上使用多个不同的a href link?和图像来自`文件夹或目录?

Php代码

<?php 

$directory = "data/uploads/bottomslider/";

if (glob($directory . "*") != false)
{

$filecount = count(glob($directory . "*"));

}
$files_index = glob("data/uploads/"."bottom"."slider/*.*");

for ($i=0; $i<$filecount; $i++)
{
$num2 = $files_index[$i];

?>  

<li class="jcarousel-item"><a href=""><img src="<?php echo $num2;?>" 
style="height: 119px!important; width:132px !important;"/></a></li>

<? }?>

例如

i want like this?

<a href="google.com">Image1</a>
<a href="facebook.com">Image2</a>
<a href="yahoo.com">Image2</a>

2 个答案:

答案 0 :(得分:1)

我没有测试代码。但这应该有效。假设文件夹“data”位于您网页的根目录中。

$directory = "data/uploads/bottomslider/";
if(file_exists($directory)) {
    $files = scandir($directory);
    $output = "";
    foreach($files as $key => $value) {
        if($value != '.' && $value != '..' && $value != '.quarantine' && $value != '.tmb') {
            $output .= '<li class="jcarousel-item"><a href="/'.$directory.$value.'"><img src="/'.$directory.$value.'"
style="height: 119px!important; width:132px !important;"/></a></li>';
        }
    }
    //$output contains all the images.
    echo $output;
}

答案 1 :(得分:0)

<?php 

$directory = "data/uploads/bottomslider/";
$link = "";
if (glob($directory . "*") != false)
{
    $filecount = count(glob($directory . "*"));
}
$files_index = glob("data/uploads/"."bottom"."slider/*.*");
//this array must be same size like number of images 
$array1 = array('www.yahoo.com', 'www.google.com', 'www.facebook.com', 'www.stackovrflow.com', 'www.blahblah.com');
for ($i=0; $i<$filecount; $i++)
{
    $num2 = $files_index[$i];
    $link = $array1[$i];

?>  

<li class="jcarousel-item"><a href="<?php echo $link?>"><img src="<?php echo $num2;?>" 
style="height: 119px!important; width:132px !important;"/></a></li>

<?php
}
?>

提供完整的链接路径,例如“https://www.facebook.com/