SunQuest
           PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old October 3rd, 2002, 12:26 AM
Inferno Inferno is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 29 Inferno User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 54 sec
Reputation Power: 0
Send a message via Yahoo to Inferno
why do i get this error?

Hi:

I was wondering if someone would be able to help me out? I have this script that calls random images form a given directory and I get this error when I run the script.

Warning: mt_rand(): Invalid range: 0..0

Can someone please help me and tell me why I get this error and how I could fix it?

Any help would be great!




Code:

$imgpath = "path/to/image/dir";
$handle = opendir( "$imgpath" );

$imgArray = array();

while($file = readdir($handle))
{
    if( $file != "." && $file != ".." )
    {
        array_push( $imgArray, $file );
    }
}
closedir( $handle );

mt_srand( (double)microtime( ) * 1000000 );
$randval = mt_rand( 0, sizeof( $imgArray ) - 1 );

print"<IMG SRC="$imgpath/" . $imgArray[ $randval ] . "">";

Reply With Quote
  #2  
Old October 3rd, 2002, 12:43 AM
Matt Matt is offline
Moderator
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 10 m 20 sec
Reputation Power: 6
RE: why do i get this error?

Hmmm..that code works fine for me...are you sure that $imgpath is correct and that atleast 2 images are in that directory?

Reply With Quote
  #3  
Old October 3rd, 2002, 12:45 AM
Inferno Inferno is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 29 Inferno User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 54 sec
Reputation Power: 0
Send a message via Yahoo to Inferno
RE: why do i get this error?

ok, it works if i have more then one image in that directory. but how would i make it so that if there is by chance only one image that i wouldnt get an error message?

-thanks matt!

Reply With Quote
  #4  
Old October 3rd, 2002, 12:49 AM
Matt Matt is offline
Moderator
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 10 m 20 sec
Reputation Power: 6
RE: why do i get this error?

Try thig:

php Code:
Original - php Code
  1. <?php
  2. $imgpath = "/www/codewalkers.com/www/images";
  3. $handle = opendir( "$imgpath" );
  4.  
  5. $imgArray = array();
  6.  
  7. while($file = readdir($handle))
  8. {
  9. if( $file != "." && $file != ".." )
  10. {
  11. array_push( $imgArray, $file );
  12. }
  13. }
  14. closedir( $handle );
  15.  
  16. mt_srand( (double)microtime( ) * 1000000 );
  17.  
  18. $max = sizeof( $imgArray ) - 1;
  19. if ($max == 0) {
  20.     $randval = 0;
  21. } else {
  22.     $randval = mt_rand( 0, $max );
  23. }
  24.  
  25. print"<IMG SRC="http://codewalkers.com/images/" . $imgArray[ $randval ] . "">";
  26. ?>


Reply With Quote
  #5  
Old October 3rd, 2002, 12:55 AM
Inferno Inferno is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 29 Inferno User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 54 sec
Reputation Power: 0
Send a message via Yahoo to Inferno
RE: why do i get this error?

great thanks a million!

now i just have to figure out how to make it so it just looks for images ;-)

Reply With Quote
  #6  
Old October 3rd, 2002, 01:05 AM
Matt Matt is offline
Moderator
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 10 m 20 sec
Reputation Power: 6
RE: why do i get this error?

Since you asked

php Code:
Original - php Code
  1. <?php
  2. $imgpath = "/www/codewalkers.com/www/images";
  3. $handle = opendir( "$imgpath" );
  4.  
  5. $imgArray = array();
  6.  
  7. $extensions = array('.gif','.jpg','.png');
  8.  
  9. while($file = readdir($handle))
  10. {
  11. if( $file != "." && $file != ".." && in_array(substr($file, -4), $extensions))
  12. {
  13. array_push( $imgArray, $file );
  14. }
  15. }
  16. closedir( $handle );
  17.  
  18. mt_srand( (double)microtime( ) * 1000000 );
  19.  
  20. $max = sizeof( $imgArray ) - 1;
  21. if ($max == 0) {
  22.     $randval = 0;
  23. } else {
  24.     $randval = mt_rand( 0, $max );
  25. }
  26.  
  27. print"<IMG SRC="http://codewalkers.com/images/" . $imgArray[ $randval ] . "">";
  28. ?>

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > why do i get this error?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway