Jump to content

Adding additional string or Caption to Random Images.


iconicCreator

Recommended Posts

 

Hello everyone,

 

I have this very short script I wrote with help from a book using arrays and the rand function.

Basically it changes the images randomly on browser load or refresh.

However, I am trying to add an additional, not sure what to call it, a string or another caption for each images.

 

Instead of this being a caption, it will be a link, so a link will appear under each caption that goes to another page

when it is clicked on.

 

I tried adding another caption and adding a like to it but getting syntax error.

 

Thanks everyone!

 

Here is the code:

<?php
$images = array(
array('file' => 'image1',
'caption' => 'Caption 1'),

array('file' => 'image2',
'caption' => 'Caption 2'),
);


$i = rand(0, count($images)-1);
$selectedImage = "graphics/{$images[$i]['file']}.png";
$caption = $images[$i]['caption'];
?>

 

And here is where the images are written to the page:

<div id="stage">
<img src="<?php echo $selectedImage; ?>" alt="Random image" />
<p id="caption"><?php echo $caption; ?></p>
</div>

 

IC

 

Hi

 

Can you give a copy of the error you're getting please

 

Hello, here is what I added to the code, however I did not add this in the original post because I did not want to post a coded causing errors.

Basically I am getting a syntax error.

 

<?php
$images = array(
array('file' => 'image1',
'caption' => 'Caption 1'),

//Added for the link, error is on this line
'link' => '<a href="#">Read More</a>'),

array('file' => 'image2',
'caption' => 'Caption 2'),
'link' => '<a href="#">Read More</a>'),
);


$i = rand(0, count($images)-1);
$selectedImage = "graphics/{$images[$i]['file']}.png";
$caption = $images[$i]['caption'];

//Assigned the link to the random image
$link = $images[$i]['link'];

?>

 

Here is the output:

<div id="stage">
<img src="<?php echo $selectedImage; ?>" alt="Random image" />
<p id="caption"><?php echo $caption; ?></p>

<!--OUTPUT LINK HERE-->
<p id="linktab1"><?php echo $link; ?></p>
</div>

 

Thanks again!

 

IC

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.