|
||||||||||
|
|||||||
| GoogleCommunity Sponsor |
Use coupon "forum" for 50% Off! |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Noogle
![]() |
Random php script
I got the idea from www.cosforums when the banner changes every new page
Code:
<?
# Put the SERVER PATH to image directory.
# INCLUDE trailing slash
$dir = "/home/deathqe/public_html/forum/randphotos/";
# Put the HTML PATH to image directory.
# DO NOT include trailing slash
$html_dir = "http://www.death-eater.net/forum/randphotos";
$dir_handle = opendir($dir);
$image_array = array();
# This opens up the image directory, grabs all files with a ".jpg" extension
# and puts them into an array. You can set whatever extension you'd like here.
# Just switch out the .jpg for .gif, etc. If you have more than one extension,
# replace this line with something like this:
# if (($file !=".") && ($file !="..") && (substr($file, -4) == ".jpg") || (substr($file, -4) == ".gif"))) {
# This will pull up all .jpg OR .gifs. If you name all the images you want to pull
# a certain way (example, image-thumb.jpg or image.thumb.jpg) you can do this:
# if (($file !=".") && ($file !="..") && (substr($file, -10) == ".thumb.jpg")) {
# basically you want to put the extension in quotes and then count how many characters
# it is and place that after $file, -
while ($file = readdir($dir_handle)) {
if (($file !=".") && ($file !="..") && (substr($file, -4) == ".jpg")) {
$file_name = "$html_dir/$file";
$image_array[$file_name] = $file_name;
}
}
# sorts the array. For some reason this won't work at all
# unless this is done. Can anyone tell me why?
sort($image_array);
$count = sizeof($image_array);
# trims all the URLs of whitespace and assigns each
# item in the array a number to identify it.
for($i = 0;$i<sizeof($image_array); $i++) {
$image[$j++] = rtrim($image_array[$i]);
}
# assign a random value
$r = rand(0,$count-1);
# get the size of a random image
$size = getimagesize($image[$r]);
$width = $size[0];
$height = $size[1];
$img_src = "<img src=\"$image[$r]\" width=\"$width\" height=\"$height\" alt=\"This is a Random Image\" />";
closedir ($dir_handle);
?>
<html>
<head>
</head>
<body>
<p align="center">
<? echo '$img_src; ?>[img]file:///C:/Documents%20and%20Settings/Owner/Desktop/banner.gif[/img]</p>
</body>
</html>
|
|
|
|
|
Sponsored Links
|
|
|
|
#5 (permalink) | |
|
Junior Googler
![]() |
Re: Random php script
Quote:
I would do this: Code:
<?
# Put the SERVER PATH to image directory.
# INCLUDE trailing slash
$dir = "/home/deathqe/public_html/forum/randphotos/";
# Put the HTML PATH to image directory.
# DO NOT include trailing slash
$html_dir = "http://www.death-eater.net/forum/randphotos";
$dir_handle = opendir($dir);
$image_array = array();
# This opens up the image directory, grabs all files with a ".jpg" extension
# and puts them into an array. You can set whatever extension you'd like here.
# Just switch out the .jpg for .gif, etc. If you have more than one extension,
# replace this line with something like this:
# if (($file !=".") && ($file !="..") && (substr($file, -4) == ".jpg") || (substr($file, -4) == ".gif")))
# {
# // This will pull up all .jpg OR .gifs. If you name all the images you want to pull
# // a certain way (example, image-thumb.jpg or image.thumb.jpg) you can do this:
# if (($file !=".") && ($file !="..") && (substr($file, -10) == ".thumb.jpg"))
# {
# // basically you want to put the extension in quotes and then count how many characters
# // it is and place that after $file, -
while ($file = readdir($dir_handle))
{
if (($file !=".") && ($file !="..") && (substr($file, -4) == ".jpg"))
{
$file_name = "$html_dir/$file";
$image_array[$file_name] = $file_name;
}
}
# sorts the array. For some reason this won't work at all
# unless this is done. Can anyone tell me why?
sort($image_array);
$count = sizeof($image_array);
# trims all the URLs of whitespace and assigns each
# item in the array a number to identify it.
for($i = 0;$i<sizeof($image_array); $i++)
{
$image[$j++] = rtrim($image_array[$i]);
}
# assign a random value
$r = rand(0,$count-1);
# get the size of a random image
$size = getimagesize($image[$r]);
$width = $size[0];
$height = $size[1];
$img_src = "<img src=\"$image[$r]\" width=\"$width\" height=\"$height\" alt=\"This is a Random Image\" />";
closedir ($dir_handle);
?>
<html>
<head>
</head>
<body>
<p align="center">
<? echo '$img_src'; ?>[img]file:///C:/Documents%20and%20Settings/Owner/Desktop/banner.gif[/img]</p>
</body>
</html>
I just think it's more organized and easier to read that way... |
|
|
|
|
|
|
#6 (permalink) |
|
Google Freak
![]() |
I recommed you to get a PHPEditor, that helps a lot
It is like VS.NET or FrontPage... Try Googling for a PHPEditor, that will fix your little details when programming... I have made several scripts that didn't work at first for the stupid errors like missing a letter or a ";" or the " in self... Hahahah programming is so perfectly design for mistakes.... Let me know about the editor :P See ya |
|
|
|
|
|
#7 (permalink) |
|
Noogle
![]() Join Date: Oct 2004
Location: Paris
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
If you have a MySQL database, you can also insert your pictures (or only their names) into, and after use the rand() function to select one random image on every new page.
Code:
SELECT pic_id FROM pictures ORDER BY rand() |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Make a random remark! | andrew247 | Games and Quizzes | 285 | 06-07-2008 09:38 AM |
| /random feature request | raduweiss | Google Talk | 4 | 12-30-2005 10:44 PM |
| ME!, dont u just love random? | mikeskuse999 | Introduce Yourself | 1 | 07-20-2005 08:47 AM |
| Random image for your gallery | puccavn | Web Design, Coding & Programming Forum | 0 | 05-05-2005 11:44 PM |
| Make a random remark! | andrew247 | Games and Quizzes | 264 | 01-01-1970 10:29 PM |