Darkmatter5 Posted April 22, 2009 Share Posted April 22, 2009 Here's my site structure public_html images backgrounds /public_html/index.php /public_html/images/backgrounds/001.jpg /public_html/images/backgrounds/002.jpg index.php has the following code <body <?php $bkgs=scandir("images/backgrounds/"); echo "backgound='images/backgrounds/" .array_rand($bkgs,1). "'"; ?> > It's not selecting anything, so I put this code in the page to test output. <?php print_r(scandir("images/backgrounds/")) ?> It gives me "Array ( [0] => . [1] => .. [2] => 001.jpg [3] => 002.jpg )" and viewing the generated pages source gives me "<body background='images/backgrounds/3' >". How do I get it to insert the filename? Quote Link to comment https://forums.phpfreaks.com/topic/155214-help-with-scandir/ Share on other sites More sharing options...
Daniel0 Posted April 22, 2009 Share Posted April 22, 2009 array_rand() returns the index of the randomly chosen element, it doesn't return the element itself. Quote Link to comment https://forums.phpfreaks.com/topic/155214-help-with-scandir/#findComment-816540 Share on other sites More sharing options...
Darkmatter5 Posted April 22, 2009 Author Share Posted April 22, 2009 So how might I get this accomplished? Quote Link to comment https://forums.phpfreaks.com/topic/155214-help-with-scandir/#findComment-816541 Share on other sites More sharing options...
Daniel0 Posted April 22, 2009 Share Posted April 22, 2009 It would be $bgks[array_rand($bkgs,1)]. However, seeing as you cannot use index 0 and 1 (they are the current and parent directory, respectively), you could do something like this: $bgks[mt_rand(2, count($bgks) - 1)] Quote Link to comment https://forums.phpfreaks.com/topic/155214-help-with-scandir/#findComment-816542 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.