所以我有一个数组,它可以获得随机图像并用特定文本显示它们。不幸的是,文本将不会出现,如果它出现,超链接不会出现在图像下方。请有人帮忙!!
<?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'] );
}
?>
答案 0 :(得分:2)
要在图像下定位链接,您必须处理CSS。为了正确处理代码示例,请进行以下更改:
将RANDOM_IMAGES_FORMAT
更新为
define('RANDOM_IMAGES_FORMAT', '<img src="%s" /><a href="%s" alt="%s" title="%s" style="margin-right:10px">Click Me</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/' )
);
像这样使用printf
:
printf( RANDOM_IMAGES_FORMAT, $tmp['src'], $tmp['href'], $tmp['title'], $tmp['title'] );
答案 1 :(得分: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
数组中,链接应该只是网址,而不是完整链接,正如您在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'] );
}
答案 2 :(得分:0)
<img src="%s"><a href="%s"> alt="%s" title="%3$s" style="margin-right:10px"></a>
在href =“%s”之后不要关闭标签a 并将img放在标签内,如下所示:
<a><img/></a>
答案 3 :(得分: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