alex3 Posted April 21, 2009 Share Posted April 21, 2009 I have two images I want to load from a folder of images, these images must match. The syntax of the image files names is headerpX.png and footerpX.png where X is a number. I'd like a random set of these images to load every page load. Here's how I think might be a good way to do it in very pseudo-code $1 get number of files in './images/' folder $2 get the integer of ($1/2) $3 get random number between 1 and $2 And then in my HTML code, I'd have the code <img src="images/headerp$3.png"></img> <img src="images/footerp$3.png"></img> (or however I'd embed that random number in to the src field) Is this do-able and easy code? Will the $3 number be the same if I call it twice in the HTML, or will it run the PHP script again and return a different random number? Link to comment https://forums.phpfreaks.com/topic/155037-solved-generating-and-using-random-numbers/ Share on other sites More sharing options...
taith Posted April 21, 2009 Share Posted April 21, 2009 <? $f=glob('images/*');#grabs all the filepaths in the images folder, sets into an array $i=$f[array_rand($f)]; #sets a random imagepath from the prior array into $i ?> <img src="<?=$i?>"> #shows the image prior set and presto! random image from the images/ on each load... Link to comment https://forums.phpfreaks.com/topic/155037-solved-generating-and-using-random-numbers/#findComment-815440 Share on other sites More sharing options...
alex3 Posted April 21, 2009 Author Share Posted April 21, 2009 Will that not load any image from the images folder though? I have a folder with header and footer images which are numbered. I need the script to essentially give me a number based on the number of images (files really, as it's only the header and footer images in the folder) that I can then use to load the header and footer image file relating to that number. Link to comment https://forums.phpfreaks.com/topic/155037-solved-generating-and-using-random-numbers/#findComment-815443 Share on other sites More sharing options...
taith Posted April 21, 2009 Share Posted April 21, 2009 ok... you can simply $c=count($f); which will give you the number of files in the specified folder... and with the glob... it can search for specific content if that helps too... $f=glob('images/header-*.jpg'); the * is just a wildcard... it'd grab header-1.jpg, header-2.jpg while leaving out header1.jpg... for example... Link to comment https://forums.phpfreaks.com/topic/155037-solved-generating-and-using-random-numbers/#findComment-815454 Share on other sites More sharing options...
alex3 Posted April 21, 2009 Author Share Posted April 21, 2009 Aha! So what I could do is this.. <? $f=glob(images/headerp*.jpg'); $i=$f[array_rand($f)]; some how return that random header image ?> and then in my HTML , just have something like <img src="randomheaderimage.php"></img> <img src="randomfooterimage.php"></img> where randomfooterimage.php is just the other PHP script but with header changed to footer. Does that sound about right? If so, how could I go about having the script return the random image? (so I can use a the .php file as the img src) Link to comment https://forums.phpfreaks.com/topic/155037-solved-generating-and-using-random-numbers/#findComment-815458 Share on other sites More sharing options...
alex3 Posted April 21, 2009 Author Share Posted April 21, 2009 OK. I've got something preliminary but I;m having hiccups. here's my PHP script: <?php $f = glob('photos/*'); $i = $f[array_rand($f)]; $h = "<img src=\"$i/header.png\" alt=\"Header\"></img>"; $f = "<img src=\"$i/footer.png\" alt=\"Footer\"></img>"; ?> I've changed the structure of my photos folder so that now it contains multiple folders, '1' '2' '3' '4' etc. Inside every one of these is two files; header.png and footer.png. The script generates a random folder number, and then picks out the header and footer files out of these. My HTML code is as follows (bit long) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" /> <?php require("random.php"); ?> <title>PHP Include Test</title> </head> <body> <?php echo "$h"; ?> </body> </html> I've got a .htaccess in the same folder as the index.html file that makes the server parse it as PHP. The trouble is, when I go to php all I see is a missing picture file icon, and for some reason it's trying to load a header.png from the root of my web space. Why is this? Link to comment https://forums.phpfreaks.com/topic/155037-solved-generating-and-using-random-numbers/#findComment-815603 Share on other sites More sharing options...
taith Posted April 21, 2009 Share Posted April 21, 2009 well... firstly you want a / at the end of your glob() $f = glob('photos/*/'); that way it will ONLY grab folders... (errors cause me headaches... best to think ahead of them ) secondly... you dont need </img> tags... and thirdly... what tags are being output? Link to comment https://forums.phpfreaks.com/topic/155037-solved-generating-and-using-random-numbers/#findComment-815758 Share on other sites More sharing options...
alex3 Posted April 21, 2009 Author Share Posted April 21, 2009 It lives! <?php $f = glob('scripts/photos/*/'); $i = $f[array_rand($f)]; //$h = "<img src=\"$i/header.png\" alt=\"Header\"></img>"; //$f = "<img src=\"$i/footer.png\" alt=\"Footer\"></img>"; ?> [code] And the HTML [code] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" /> <? require_once("scripts/random.php"); ?> <title>Test2</title> </head> <body> <div id="header"><img alt="Header" src="<?="$i";?>header.png"></img></div> <div id="footerbg"><img alt="Footer" src="<?="$i";?>footer.png" /></div> </body> </html> Taith, what I was trying to do with create a string so that I didn't need to add header/footer.png in to my img tags in the HTML. I would would still love to know how to do it for cleaner code though. Thanks for the amazing help so far though, you've been brilliant. Link to comment https://forums.phpfreaks.com/topic/155037-solved-generating-and-using-random-numbers/#findComment-815780 Share on other sites More sharing options...
taith Posted April 21, 2009 Share Posted April 21, 2009 as far as clean code... thats completly subjective... i would say, having it directly into the html means that the php parser doesnt have to touch it... as far as your prior setup... i'd say... $h=glob('scripts/photos/header-*.jpg'); $f=glob('scripts/photos/footer-*.jpg'); $i=rand(1,count($h)); echo '<img src="'.$h[$i].'">'; echo '<img src="'.$f[$i].'">'; that may be simpler in fashion, yet more complex in function...(more chance of variables being overwritten between parsing, and outputting) Link to comment https://forums.phpfreaks.com/topic/155037-solved-generating-and-using-random-numbers/#findComment-815790 Share on other sites More sharing options...
The Little Guy Posted April 21, 2009 Share Posted April 21, 2009 I have always done this to generate a random image to display on the page: http://beta.phpsnips.com/snippet.php?id=14 Link to comment https://forums.phpfreaks.com/topic/155037-solved-generating-and-using-random-numbers/#findComment-815791 Share on other sites More sharing options...
taith Posted April 21, 2009 Share Posted April 21, 2009 I have always done this to generate a random image to display on the page: http://beta.phpsnips.com/snippet.php?id=14 that'd be my first suggestion... he just wanted the header/footer to be of the same number... which has to be a touch more complicated... Link to comment https://forums.phpfreaks.com/topic/155037-solved-generating-and-using-random-numbers/#findComment-815793 Share on other sites More sharing options...
alex3 Posted April 21, 2009 Author Share Posted April 21, 2009 Yeah I was able to get quite a few random image PHP scripts, but I had to have the two images match. Taith's solution worked out best for me; get the script to randomly select from one of many folders (each with a separate header and footer image inside) and then use that selected folder in an img tag. Very happy it finally works! Link to comment https://forums.phpfreaks.com/topic/155037-solved-generating-and-using-random-numbers/#findComment-815798 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.