Reputation: 33
I have to display random photo using array and rand function.
Now I would add text under each photo(For example, the words of the elders text with image)
my code:
<?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);
?>
view in htm
<img src="<?php echo $selectimage;?> " alt="<?php echo $alt ; ?>" <?php echo $imagesize[3] ; ?> />
What do I do?
Upvotes: 0
Views: 99
Reputation: 33
I found.
for display text should add text to array.
<!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>
Upvotes: 0
Reputation: 839
Easiest way I know of is to wrap the image and text then position accordingly.
<?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>
Upvotes: 1
Reputation: 4228
you have syntax error in your code. you need to use ,
instead of ;
in array and use ;
at the end of each statement.
$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")
);
Upvotes: 1