-4

所以我有一个数组,它获取随机图像并用某些文本显示它们。不幸的是,文本不会出现,如果出现,超链接不会出现在图像下方。请有人帮忙!!

<?php
define('RANDOM_IMAGES_COUNT', 3);
define('RANDOM_IMAGES_FORMAT', '<img src="%s"><a href="%s"> alt="%s" title="%3$s"   style="margin-right:10px"></a>');

#------------------------------------------------------------------------------

$images = array (
    array ( 'title' => 'Test 2', 'src' => 'pic2.jpg', 'href' => '<a href=http://mylink.com/path/>Click Me</a>' ),
    array ( 'title' => 'Test 3', 'src' => 'pic3.jpg', 'href' => '<a href=http://mylink.com/path/>Click Me</a>' ),
    array ( 'title' => 'Test 4', 'src' => 'pic4.jpg', 'href' => '<a     href=http://mylink.com/path/>Click Me</a>' )
);

#------------------------------------------------------------------------------

if ( count($images) < RANDOM_IMAGES_COUNT ) {
    trigger_error('Not enough images given', E_USER_WARNING);
    exit;
}

#------------------------------------------------------------------------------

for ($i = 0; $i < RANDOM_IMAGES_COUNT; $i++) {
    shuffle($images);

    $tmp = array_shift($images);
    printf( RANDOM_IMAGES_FORMAT, $tmp['src'],$tmp['href'], $tmp['title'] );
}
?>
4

4 回答 4

2

为了在图片下定位链接,您必须使用 CSS。为了使代码示例正常工作,请进行以下更改:

  1. 更新RANDOM_IMAGES_FORMAT

    define('RANDOM_IMAGES_FORMAT', '<img src="%s" /><a href="%s" alt="%s" title="%s" style="margin-right:10px">Click Me</a>');
    
  2. 将数组更改为:

    $images = array (
        array ( 'title' => 'Test 2', 'src' => 'pic2.jpg', 'href' => 'http://mylink.com/path/' ),
        array ( 'title' => 'Test 3', 'src' => 'pic3.jpg', 'href' => 'http://mylink.com/path/' ),
        array ( 'title' => 'Test 4', 'src' => 'pic4.jpg', 'href' => 'http://mylink.com/path/' )
    );
    
  3. 像这样使用printf

    printf( RANDOM_IMAGES_FORMAT, $tmp['src'], $tmp['href'], $tmp['title'], $tmp['title'] );
    
于 2013-09-03T09:21:53.013 回答
1

您的代码有多个问题:

常量RANDOM_IMAGES_FORMAT的 HTML 格式很差,它应该如下所示:

define('RANDOM_IMAGES_FORMAT', '<img src="%s" alt="%s" title="%3$s"   style="margin-right:10px"><a href="%s"></a>');

至于那个, printf 参数的顺序应该重新排序,因为我们已经修改了显示它们RANDOM_IMAGES_FORMAT

printf( RANDOM_IMAGES_FORMAT, $tmp['src'],$tmp['href'], $tmp['title'] );

在您的$images数组中,链接应该只是 URL,而不是完整链接,正如您在RANDOM_IMAGES_FORMAT.

您提供的完整代码应如下所示:

define('RANDOM_IMAGES_COUNT', 3);
define('RANDOM_IMAGES_FORMAT', '<img src="%s"  alt="%s" title="%3$s"   style="margin-right:10px"><a href="%s"></a>');

$images = array (
    array ( 'title' => 'Test 2', 'src' => 'pic2.jpg', 'href' => 'http://mylink.com/path/' ),
    array ( 'title' => 'Test 3', 'src' => 'pic3.jpg', 'href' => 'http://mylink.com/path/' ),
    array ( 'title' => 'Test 4', 'src' => 'pic4.jpg', 'href' => 'http://mylink.com/path/' )
);


if ( count($images) < RANDOM_IMAGES_COUNT ) {
    trigger_error('Not enough images given', E_USER_WARNING);
    exit;
}


for ($i = 0; $i < RANDOM_IMAGES_COUNT; $i++) {
    shuffle($images);

    $tmp = array_shift($images);
    printf( RANDOM_IMAGES_FORMAT, $tmp['src'],$tmp['href'], $tmp['title'] );
}
于 2013-09-03T09:23:42.327 回答
0

查看

 define('RANDOM_IMAGES_FORMAT', '<img src="%s"><a href="%s"> alt="%s" title="%3$s"   style="margin-right:10px"></a>');

 array ( 'title' => 'Test 2', 'src' => 'pic2.jpg', 'href' => '<a href=http://mylink.com/path/>Click Me</a>' ),

他们是href尝试只提供链接的问题href

于 2013-09-03T09:20:21.273 回答
0
<img src="%s"><a href="%s"> alt="%s" title="%3$s"   style="margin-right:10px"></a>

不要在 href="%s" 之后关闭标签 a 并将 img 放在标签内,如下所示:

<a><img/></a>
于 2013-09-03T09:18:58.853 回答