FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » displaying random images with an appropriate caption
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: displaying random images with an appropriate caption [message #175937 is a reply to message #175934] Wed, 09 November 2011 12:06 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On Tue, 08 Nov 2011 19:54:42 -0800, paris2venice wrote:

> I just want the captions in an array just like the filenames for the
> images. I just don't know how to make the randomizer get the match from
> each array.

given two text files, one with a list of image paths and names based on
webroot, the other with a list of captions, so that 7th caption in the
caption file goes with the 7th url in the images file, and images and
captions are each listed on a single line in their respective files:

<?php

$flags = FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES;

$captions = file("captionfile",$flags); // read array of captions, 1 per
line
$images = file("imagesfile", $flags); // read array of filenames, 1 per
line

$index = rand(0, count($captions)-1); // get a random index

$imgfile = $images[$index];
if (strpos($mgfile,"/")!==0) $imgfile = "/" . $imgfile;
$captiontext = ucwords($captions[$index]);

echo <<<EOT

<p style="text-align:center;margin:0 auto">
<img src="http://www.myhost.domain{imgfile}" alt="{$captiontext}"><br>
{$captiontext}</p>

EOT;

?>

imagesfile might contain:

---8<--- cut here ---8<---
/dir/dir/myface.jpg
/dir2/clown.jpg

lorry.png

/dir/dir3/cat.gif
/dir/dir3/dog.gif

---8<--- cut here ---8<---

captionfile might contain:

---8<--- cut here ---8<---

a mugshot, so if you see me you'll know me
my clown face
the lorry I drive

my pet cat

the dog next door
---8<--- cut here ---8<---

Rgds

Denis McMahon
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: php development environment in windows problems
Next Topic: Images retrives
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 04:31:03 GMT 2024

Total time taken to generate the page: 0.03783 seconds