我必须使用数组和rand函数显示随机照片。
现在我要在每张照片下面添加文字(例如,长者用文字加文字)
我的代码:
<?php
$images=array(
array('file'=>'pic1','alt'=>"Your Description About pic1"),
array('file'=>'pic2','alt'=>"Your Description About pic2"),
array('file'=>'pic3','alt'=>"Your Description About pic3"),
array('file'=>'pic4','alt'=>"Your Description About pic4"),
array('file'=>'pic5','alt'=>"Your Description About pic5"),
array('file'=>'pic6','alt'=>"Your Description About pic6"),
)
$i=rand(0,count($images)-1);
$selectimage="newfolder/{$images[$i]['file']}.jpg";
$alt=$images[$i]['alt'];
if (file_exists($selectimage) && is_readable($selectimage))
$imagesize= getimagesize($selectimage);
?>
在htm中查看
<img src="<?php echo $selectimage;?> " alt="<?php echo $alt ; ?>" <?php echo $imagesize[3] ; ?> />
我该怎么办?
答案 0 :(得分:1)
我知道的最简单的方法是将图像和文本换行,然后相应地定位。
<?php
$images = array(
array('file'=>'pic1','alt'=>"Your Description About pic1", 'text'=>"Pic1 Subtext"),
array('file'=>'pic2','alt'=>"Your Description About pic2", 'text'=>"Pic1 Subtext")
);
$i=rand(0,count($images)-1);
$selectimage="newfolder/{$images[$i]['file']}.jpg";
$alt=$images[$i]['alt'];
$subtitle = $images[$i]['text'];
if (file_exists($selectimage) && is_readable($selectimage)) {
$imagesize= getimagesize($selectimage);
}
?>
<div class="image_wrapper">
<img src="<?= $selectimage; ?>" alt="<?= $alt; ?>"/>
<p class="img_subtitle"><?= $subtitle; ?></p>
</div>
答案 1 :(得分:1)
您的代码中存在语法错误。您需要在数组中使用,
而不是;
,并在每个语句的末尾使用;
。
$images=array(
array('file'=>'pic1','alt'=>"Your Description About pic1"),
array('file'=>'pic2','alt'=>"Your Description About pic2"),
array('file'=>'pic3','alt'=>"Your Description About pic3"),
array('file'=>'pic4','alt'=>"Your Description About pic4"),
array('file'=>'pic5','alt'=>"Your Description About pic5"),
array('file'=>'pic6','alt'=>"Your Description About pic6")
);
答案 2 :(得分:0)
我找到了。
显示文本的应该将文本添加到数组中。
<!doctype html>
<html>
<head>
<?php
$images= array(
array('file' => 'pic1' ,'txt'=>'sample1', 'alt' => "Your Discription About pic1"),
array('file' => 'pic2','txt'=>'sample2' , 'alt '=> "Your Discription About pic2"),
array('file'=> 'pic3' ,'txt'=>'sample3', 'alt' => "Your Discription About pic3"),
array('file' => 'pic4','txt'=>'sample4' , 'alt' => "Your Discription About pic4"),
array('file'=>'pic5','txt'=>'sample5' , 'alt' => "Your Discription About pic5")
);
$i=rand(0,count($images)-1);
$selectimage="newfolder/{$images[$i]['file']}.jpg";
$alt=$images[$i]['alt'];
if (file_exists($selectimage) && is_readable($selectimage)) {
$imagesize= getimagesize($selectimage);
}
?>
</head>
<body>
<img src="<?php echo $selectimage;?> " />
<?php echo $images[$i]['txt'];?>
</body>
</html>