Jump to content

[SOLVED] Generating and using random numbers


alex3

Recommended Posts

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
Share on other sites

<?
$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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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 :D)

 

secondly... you dont need </img> tags...

 

and thirdly... what tags are being output?

Link to comment
Share on other sites

It lives!  ;D

 

<?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
Share on other sites

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
Share on other sites

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