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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.