Google Community Chat
 
Random php script

You're visiting Google Community as a guest.
In order to post, you'll need to register and log in.


(If you were registered and logged in, these advertisements wouldn't be here)
Post new topic   Reply to topic    Google Community Forum Index // Web Design, Coding & Programming Forum
   
Author Message
kaz
Noogle
Noogle


Joined: 23 Jul 2004
Location: Manchester
93.96 GC$

Items

PostPosted: Fri Jul 23, 2004 3:30 am    Post subject: Random php script Reply with quote
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 border="0" src="file:///C:/Documents%20and%20Settings/Owner/Desktop/banner.gif" width="385" height="140"></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
_________________

Maybe in the future I might just get that address ..
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
intelliot
Site Admin


Joined: 01 May 2004

18653.45 GC$

Items

PostPosted: Fri Jul 23, 2004 3:53 pm    Post subject: Reply with quote
Make sure you upload it in ASCII mode.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
kaz
Noogle
Noogle


Joined: 23 Jul 2004
Location: Manchester
93.96 GC$

Items

PostPosted: Sat Jul 24, 2004 10:30 am    Post subject: Reply with quote
i did. i found the problem non the less lol <? $img src ?> i had a " before and not one ending Wink
_________________

Maybe in the future I might just get that address ..
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
tjoyce
Junior Googler
Junior Googler


Joined: 03 Jul 2004

732.15 GC$

Items

PostPosted: Sun Jul 25, 2004 8:42 am    Post subject: Reply with quote
...
Glad you could get it!
I'd recommend formatting your statements so it's easier to read... see my next post.
_________________
Tom Joyce
tomjoyce@gmail.com
http://sharethegmailinvites.blogspot.com/
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number
tjoyce
Junior Googler
Junior Googler


Joined: 03 Jul 2004

732.15 GC$

Items

PostPosted: Sun Jul 25, 2004 8:45 am    Post subject: Re: Random php script Reply with quote
kaz wrote:
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 border="0" src="file:///C:/Documents%20and%20Settings/Owner/Desktop/banner.gif" width="385" height="140"></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 border="0" src="file:///C:/Documents%20and%20Settings/Owner/Desktop/banner.gif" width="385" height="140"></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...
_________________
Tom Joyce
tomjoyce@gmail.com
http://sharethegmailinvites.blogspot.com/
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number
neno.tu
Google Freak
Google Freak


Joined: 10 Oct 2004
Location: Caracas - Venezuela
5951.00 GC$

Items

PostPosted: Mon Oct 11, 2004 8:32 am    Post subject: Reply with quote
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 Razz

See ya
_________________


NenO-tU Freelancer Web Designer and Programmer
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
jahsound
Noogle
Noogle


Joined: 08 Oct 2004
Location: Paris
355.05 GC$

Items

PostPosted: Wed Oct 13, 2004 12:42 am    Post subject: Reply with quote
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()

_________________
Back to top
View user's profile Send private message Visit poster's website
Sponsored Links
Posted: 5 Dec 2008 9:20 am    Post subject: Advertisements
Back to top
Post new topic   Reply to topic    Google Community Forum Index // Web Design, Coding & Programming Forum All times are GMT - 8 Hours
Page 1 of 1


 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Host your free forums with Invision Plus.net forum web hosting with your own subdomain.

alexisBlue v1.2 // Theme Created By: Andrew Charron // Icons in Part By: Travis Carden

© 2005-2006 Google Community

Powered by phpBB

Privacy Policy | Contact Us

Powered by Google Search blog

This website is not affiliated in any way with Google, Inc.
Google™ is a registered trademark of Google, Inc.