[PHP] random html +(hi)

2004-01-16 Thread Angela K Hilton
Hi All

I'm new to this list and I suppose fairly new to PHP - and hope that
this question is in keeping with the type of thing that's usually asked.

I'm trying to dynamically randomise a chunk of HTML that is used on a
page, aka  random image, so that each time a client opens the page [or
refreshes] a part of the page changes.  

The chunks of HTML are saved in their own directory as .htm files.  I
was hoping to adapt the following code I have managed to cobble together
to display random images:

?php
// images in folder
$total = 5;

// file type
$file_type = .htm, .gif, .jpg;

// dir location
$image_folder = dir/dir;

$start = 1;

$random = mt_rand($start, $total);

$image_name = $random . $file_type;

// print file name
// echo $image_name;

// display image
// echo img src=\$image_folder/$image_name\ alt=\$image_name\ /;

?

I'd like the code to take the HTML and place it in a div/div,
probably using a ?php require (); ? (I think I'm OK with this once
the code is sorted).  

However - I don't seem to be able to progress any further with this -
I'm sure I'm missing something very basic - but I just can't get past
this point.

I'd be grateful for any pointers with this.

TIA
ange

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] random html +(hi)

2004-01-16 Thread Richard Davey
Hello Angela,

Friday, January 16, 2004, 11:30:16 AM, you wrote:

AKH ?php
AKH // images in folder
AKH $total = 5;

AKH // file type
AKH $file_type = .htm, .gif, .jpg;

AKH // dir location
AKH $image_folder = dir/dir;

AKH $start = 1;

AKH $random = mt_rand($start, $total);

AKH $image_name = $random . $file_type;

AKH // print file name
AKH // echo $image_name;

AKH // display image
AKH // echo img src=\$image_folder/$image_name\ alt=\$image_name\ /;

?

AKH I'd like the code to take the HTML and place it in a div/div,
AKH probably using a ?php require (); ? (I think I'm OK with this once
AKH the code is sorted).  

Your code will produce the following image filename:

5.htm, .gif, .jpg

(assuming the random number you get is 5)

Which probably isn't what you want :)

There are a couple of ways to do this. One of the easiest would be to
assume that you have all random images in one specific directory
(called randpics for the sake of the following code). Then your
script can read out a list of files in that directory and pick one at
random and display it. The following should do the trick:

html
head
titleRandom Images/title
/head

body

?php 
$random_images_directory = randpics;

if ($handle = opendir($random_images_directory)) {
while (false !== ($file = readdir($handle))) { 
if ($file != .  $file != ..) { 
$random_images[] = $file;
} 
}
closedir($handle); 
}

/*
All of the files in that directory are now in the array $random_images
so let's pick one to display:
*/

$image = $random_images[mt_rand(0, count($random_images)-1)];
?

div align=center
img src=randpics/? echo $image? border=0 alt=Random!
/div

/body
/html

Caveat: This will only work if you only have image files in the random
images directory. If you had say an mp3 in there, it would try to
display it :) So if this isn't going to be the case then you need to
add a further check into the while loop to check the extension of the
file before adding it to the $random_images array.

count returns the number of values in an array including that held at
location 0, that is why I minus 1 from the count() function when
getting a random image.

Sorry that this doesn't actually use your original script, but this
way is a lot less maintenance - you can just upload a whole bunch of
new images into the folder and the script will see them automatically
without needing to mess with a $total variable.

-- 
Best regards,
 Richardmailto:[EMAIL PROTECTED]

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] random html +(hi)

2004-01-16 Thread memoimyself
Hello Angela,

On 16 Jan 2004 at 11:30, Angela K Hilton wrote:

 I'm trying to dynamically randomise a chunk of HTML that is used on a
 page, aka  random image, so that each time a client opens the page [or
 refreshes] a part of the page changes.  
 
 The chunks of HTML are saved in their own directory as .htm files.  I
 was hoping to adapt the following code I have managed to cobble together
 to display random images:
 
 ?php
 // images in folder
 $total = 5;

If the value of $total is not going to change, why even use a variable?
 
 // file type
 $file_type = .htm, .gif, .jpg;

What's this $file_type for?
 
 // dir location
 $image_folder = dir/dir;
 
 $start = 1;

Again, if the value of $start is not going to change, why use a variable?
 
 $random = mt_rand($start, $total);
 
 $image_name = $random . $file_type;

This won't work. You'll end up with something like $image_name = 2.htm, .gif, .jpg;, 
which is clearly not what you want (or need).

 // print file name
 // echo $image_name;
 
 // display image
 // echo img src=\$image_folder/$image_name\ alt=\$image_name\ /;
 
 ?
 
 I'd like the code to take the HTML and place it in a div/div,
 probably using a ?php require (); ? (I think I'm OK with this once
 the code is sorted).  

Suggestion: have your PHP script open the image folder, read it's contents and load 
all 
file names it finds onto an array; then user array_rand() to randomly pick a key. 
You'll 
need to get the PHP manual (in case you still don't have it) from 
http://www.php.net/download-docs.php and read up on opendir(), readdir() and 
array_rand().

Good luck,

Erik