iconicCreator Posted April 15, 2011 Share Posted April 15, 2011 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 https://forums.phpfreaks.com/topic/233780-adding-additional-string-or-caption-to-random-images/ Share on other sites More sharing options...
Nodral Posted April 15, 2011 Share Posted April 15, 2011 Hi Can you give a copy of the error you're getting plz Link to comment https://forums.phpfreaks.com/topic/233780-adding-additional-string-or-caption-to-random-images/#findComment-1202009 Share on other sites More sharing options...
iconicCreator Posted April 15, 2011 Author Share Posted April 15, 2011 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 https://forums.phpfreaks.com/topic/233780-adding-additional-string-or-caption-to-random-images/#findComment-1202017 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.