Google Community
Latest Forums Rules Resources
Custom Search

Go Back   Google Community > Webmaster Forums > Web Design, Coding & Programming Forum

GoogleCommunity Sponsor
Cirtex Hosting
Use coupon "forum" for 50% Off!

Reply
 
LinkBack Thread Tools Display Modes
Old 07-23-2004, 03:30 AM   #1 (permalink)
kaz
Noogle
 
Join Date: Jul 2004
Location: Manchester
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
kaz
Send a message via MSN to kaz
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>
But I keep getting a parse error , i dont undertand when i wrote the code and tested on my machine server it was fine
kaz is offline   Reply With Quote
 
Sponsored Links
Old 07-23-2004, 03:53 PM   #2 (permalink)
Google Guru
 
intelliot's Avatar
 
Join Date: May 2004
Posts: 2,527
Thanks: 15
Thanked 49 Times in 28 Posts
intelliot has disabled reputation
Make sure you upload it in ASCII mode.
intelliot is offline   Reply With Quote
Old 07-24-2004, 10:30 AM   #3 (permalink)
kaz
Noogle
 
Join Date: Jul 2004
Location: Manchester
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
kaz
Send a message via MSN to kaz
i did. i found the problem non the less lol <? $img src ?> i had a " before and not one ending
kaz is offline   Reply With Quote
Old 07-25-2004, 08:42 AM   #4 (permalink)
Junior Googler
 
Join Date: Jul 2004
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
tjoyce
Send a message via ICQ to tjoyce Send a message via AIM to tjoyce Send a message via MSN to tjoyce Send a message via Yahoo to tjoyce
...
Glad you could get it!
I'd recommend formatting your statements so it's easier to read... see my next post.
tjoyce is offline   Reply With Quote
Old 07-25-2004, 08:45 AM   #5 (permalink)
Junior Googler
 
Join Date: Jul 2004
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
tjoyce
Send a message via ICQ to tjoyce Send a message via AIM to tjoyce Send a message via MSN to tjoyce Send a message via Yahoo to tjoyce
Re: Random php script

Quote:
Originally Posted by kaz
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>
But I keep getting a parse error , i dont undertand when i wrote the code and tested on my machine server it was fine

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>
... Note the for loop and the while statement, etc.

I just think it's more organized and easier to read that way...
tjoyce is offline   Reply With Quote
Old 10-11-2004, 08:32 AM   #6 (permalink)
Google Freak
 
neno.tu's Avatar
 
Join Date: Oct 2004
Location: Caracas - Venezuela
Posts: 365
Thanks: 0
Thanked 0 Times in 0 Posts
neno.tu
Send a message via AIM to neno.tu Send a message via MSN to neno.tu Send a message via Yahoo to neno.tu
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
neno.tu is offline   Reply With Quote
Old 10-13-2004, 12:42 AM   #7 (permalink)
Noogle
 
Join Date: Oct 2004
Location: Paris
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
jahsound
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()
jahsound is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -8. The time now is 04:31 AM.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
© 2004–2007 Google Community