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 #175963 is a reply to message #175937] Fri, 11 November 2011 06:27 Go to previous messageGo to previous message
paris2venice is currently offline  paris2venice
Messages: 9
Registered: October 2011
Karma:
Junior Member
On Nov 9, 4:06 am, Denis McMahon <denismfmcma...@gmail.com> wrote:
> 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

Hi Denis,

Thanks a bunch for your help. I liked your solution a lot and tried
to implement it with only partial success. Below is the URL for a
test page of what I have implemented with your code. The page
appeared blank until I commented out the here document. I also
implemented var_dumps on the variables to be sure I was getting
accurate data.

http://www.geol.ucsb.edu/graduate/aa.php

I also corrected a couple of syntax errors but mostly the code is
exactly as in your code above.

This is what is commented out:

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

As you can see, it appears fine with the here doc commented. I just
can't see what is wrong with the here doc. Thanks for any further
help.
[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: Fri Nov 22 19:44:02 GMT 2024

Total time taken to generate the page: 0.04584 seconds