【问题标题】:how to use multiple different a href link on each different images?如何在每个不同的图像上使用多个不同的 a href 链接?
【发布时间】:2013-10-14 00:47:48
【问题描述】:

如何在每个不同的图像上使用多个不同的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>

【问题讨论】:

  • @Ashish 是的 $num2contains 图像路径,但我想在每个不同的图像上使用 a href lin like this google.com,yahoo.com
  • 所以最好你制作链接数组,但请记住你的链接数组必须与 $filecount 的大小相同。你可以使用相同的循环并从数组访问链接
  • @ashish 但我怎么能在不同的$num2上调用链接

标签: php arrays image indexing href


【解决方案1】:
<?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/

【讨论】:

    【解决方案2】:

    我没有测试代码。但这应该有效。假设文件夹“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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      相关资源
      最近更新 更多